Skip to content

[daemon] register the signal handler immediately on start #4016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

xmkg
Copy link
Member

@xmkg xmkg commented Mar 28, 2025

The signal handler should be the first thing to register since the controller (e.g. user, snapd) can send signals immediately. Right now, the daemon either freezes or crashes when it's being launched and then immediately killed via Ctrl-C, which can also observed via running snap restart multipass in quick succession.

The signal handler uses QCoreApplication::quit() to wrap everything up, but use of QCoreApplication::quit() requires proper initialization of the QT resources first, so the signal handler should wait until that's done.

In order to ensure that this patch does the following:

  • signal.h is moved from test/ to multipass/
  • Added a top-level mp::Signal object to facilitate communication between the signal handler thread and the main thread
  • mp::top_catch_all can now accommodate a callable as a fallback_return, so the code can now execute additional logic when the wrappee throws
  • Added QThreadPool::globalInstance()->waitForDone() to ensure that the tasks dispatched through QConcurrent::run() are waited for completion. (otherwise QCoreApplication's destructor might block)

MULTI-1918

@xmkg xmkg requested a review from Sploder12 March 28, 2025 14:36
@xmkg xmkg force-pushed the bugfix/daemon-register-signal-handlers-before-everything branch 3 times, most recently from 722f89f to 0f6bc23 Compare March 28, 2025 22:08
Copy link

codecov bot commented Mar 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.53%. Comparing base (392ba43) to head (7c6a31c).
⚠️ Report is 3429 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4016   +/-   ##
=======================================
  Coverage   89.52%   89.53%           
=======================================
  Files         260      261    +1     
  Lines       14749    14759   +10     
=======================================
+ Hits        13204    13214   +10     
  Misses       1545     1545           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sploder12
Sploder12 previously approved these changes Apr 3, 2025
Copy link
Contributor

@Sploder12 Sploder12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I was unable to hang the daemon during start!

@xmkg
Copy link
Member Author

xmkg commented Apr 8, 2025

@andrei-toterman @ricab @georgeliao this one needs a secondary review to land. Could I interest any of you :)

@georgeliao georgeliao self-requested a review April 10, 2025 13:28
Copy link
Contributor

@georgeliao georgeliao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xmkg Excellent work, the notifying after daemon construction and QObject::connect, eventually made the monitor_signals wait for it definitely make sense. Functional testing also went well. I only have left some questions for clarification.

georgeliao
georgeliao previously approved these changes Apr 25, 2025
@georgeliao georgeliao added this pull request to the merge queue Apr 25, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 25, 2025
@georgeliao georgeliao added this pull request to the merge queue Apr 25, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 25, 2025
@xmkg xmkg added this pull request to the merge queue Apr 25, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 25, 2025
@georgeliao georgeliao added this pull request to the merge queue Apr 25, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 25, 2025
@georgeliao georgeliao added this pull request to the merge queue Apr 25, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 25, 2025
@xmkg xmkg added this pull request to the merge queue Apr 28, 2025
github-merge-queue bot pushed a commit that referenced this pull request Apr 28, 2025
…-handlers-before-everything

[daemon] register the signal handler immediately on start
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 28, 2025
@georgeliao georgeliao added this pull request to the merge queue Apr 30, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 30, 2025
@georgeliao georgeliao added this pull request to the merge queue Apr 30, 2025
@ricab
Copy link
Collaborator

ricab commented Apr 30, 2025

Aren't there actual compilation issues here? e.g.:

‘Signal’ is not a member of ‘mpt’

@xmkg
Copy link
Member Author

xmkg commented Apr 30, 2025

Aren't there actual compilation issues here? e.g.:

‘Signal’ is not a member of ‘mpt’

Yes, there is. The pipeline was green before, IDK what happened. I'll push another commit to fix it.

@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 30, 2025
@georgeliao
Copy link
Contributor

@ricab nice catch, I browsed the error log several times and did not find anything. Besides, the failure occurred on the debug build only. Therefore, I thought it was the disk space thing that should have been fixed on main. Now it appeared to be something else.

@xmkg xmkg added this pull request to the merge queue Apr 30, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 30, 2025
xmkg added 2 commits April 30, 2025 17:41
The signal handler should be the first thing to register since the
controller (e.g. user, snapd) can send signals immediately. Right now,
the daemon either freezes or crashes when it's being launched and then
immediately killed via Ctrl-C, which can also observed via running
`snap restart multipass` in quick succession.

The signal handler uses `QCoreApplication::quit()` to wrap everything
up, but use of `QCoreApplication::quit()` requires proper initialization
of the QT resources first, so the signal handler should wait until that's
done.

In order to ensure that this patch does the following:

- signal.h is moved from test/ to multipass/
- Added a top-level mp::Signal object to facilitate communication between
  the signal handler thread and the main thread
- mp::top_catch_all can now accommodate a callable as a fallback_return, so
  the code can now execute additional logic when the wrappee throws
- Added `QThreadPool::globalInstance()->waitForDone()` to ensure that the
  tasks dispatched through QConcurrent::run() are waited for completion.
  (otherwise QCoreApplication's destructor might block)

Signed-off-by: Mustafa Kemal Gilor <mustafa.gilor@canonical.com>
Signed-off-by: Mustafa Kemal Gilor <mustafa.gilor@canonical.com>
@xmkg xmkg dismissed stale reviews from georgeliao and Sploder12 via 7c6a31c April 30, 2025 14:55
@xmkg xmkg force-pushed the bugfix/daemon-register-signal-handlers-before-everything branch from 0f6bc23 to 7c6a31c Compare April 30, 2025 14:55
@xmkg
Copy link
Member Author

xmkg commented Apr 30, 2025

@ricab nice catch, I browsed the error log several times and did not find anything. Besides, the failure occurred on the debug build only. Therefore, I thought it was the disk space thing that should have been fixed on main. Now it appeared to be something else.

Apparently, "main" had a mpt::Signal reference recently introduced in another MP, and since this branch was not rebased to main, and there were no merge conflicts, the PR's pipeline was green, whereas the merge queue's is not.

@xmkg xmkg requested review from georgeliao and Sploder12 May 2, 2025 08:58
@georgeliao georgeliao added this pull request to the merge queue May 2, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 2, 2025
@xmkg xmkg added this pull request to the merge queue May 2, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 2, 2025
@georgeliao georgeliao added this pull request to the merge queue May 2, 2025
Merged via the queue into main with commit f064787 May 2, 2025
15 checks passed
@georgeliao georgeliao deleted the bugfix/daemon-register-signal-handlers-before-everything branch May 2, 2025 18:39
@ricab ricab added this to the 1.16.0 milestone Jun 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants