Skip to content

Conversation

xmkg
Copy link
Member

@xmkg xmkg commented Mar 27, 2025

The daemon object hosts to QFuture and QFutureWatcher objects, which may be still active while the daemon is being destructed.

This patch ensures that the destructor call waits for all outstanding futures, and all the raised signals during the lifetime of the futures are delivered to the recipients.

MULTI-1911

Copy link

codecov bot commented Mar 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.53%. Comparing base (261158c) to head (90099bd).
Report is 3396 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4010      +/-   ##
==========================================
+ Coverage   89.34%   89.53%   +0.19%     
==========================================
  Files         260      260              
  Lines       14738    14741       +3     
==========================================
+ Hits        13167    13198      +31     
+ Misses       1571     1543      -28     

☔ 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.

*/
for (auto& [_, watcher] : async_future_watchers)
{
watcher->waitForFinished();
Copy link
Member Author

@xmkg xmkg Mar 27, 2025

Choose a reason for hiding this comment

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

This line is not always going to be hit by the unit tests since it depends on the timing. The unit test ensure_that_on_restart_future_completes should ensure that there's always a watcher alive in the destructor.

@xmkg xmkg force-pushed the bugfix/ensure-daemon-waits-for-outstanding-futures branch from 5865dff to 450087e Compare March 27, 2025 18:07
@xmkg xmkg requested a review from georgeliao March 27, 2025 18:08
* object, which makes it troublesome to do this in the AsyncPeriodicDownloadTask
* destructor.
*/
update_manifests_all_task.shutdown();
Copy link
Member Author

Choose a reason for hiding this comment

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

This fixes the notorious sporadic CI daemon unit test crash.

@xmkg xmkg force-pushed the bugfix/ensure-daemon-waits-for-outstanding-futures branch 2 times, most recently from d5e72ec to 0802da0 Compare March 28, 2025 22:05
@ricab ricab requested a review from levkropp April 4, 2025 15:14
@xmkg
Copy link
Member Author

xmkg commented Apr 9, 2025

Caught another crash related to this. It happens when the daemon constructor throws during a VM construction:

image

@georgeliao
Copy link
Contributor

Caught another crash related to this. It happens when the daemon constructor throws during a VM construction:

@xmkg For reference only, one trick we can use is a private constructor which does not throw before the main body of the daemon constructor. In this case, if the daemon constructor throws, the destructor is called.

@xmkg
Copy link
Member Author

xmkg commented Apr 9, 2025

Caught another crash related to this. It happens when the daemon constructor throws during a VM construction:

@xmkg For reference only, one trick we can use is a private constructor which does not throw before the main body of the daemon constructor. In this case, if the daemon constructor throws, the destructor is called.

Hi @georgeliao, thanks for chiming in -- excellent idea! It's a nice trick, and could work in our scenario. I tried it and it works like a charm :)

https://gcc.godbolt.org/z/oYPcvvaP1

xmkg added 4 commits April 9, 2025 15:57
The daemon object hosts to QFuture and QFutureWatcher objects,
which may be still active while the daemon is being destructed.

This patch ensures that the destructor call waits for all outstanding
futures, and all the raised signals during the lifetime of the futures
are delivered to the recipients.

Signed-off-by: Mustafa Kemal Gilor <mustafa.gilor@canonical.com>
Added the `ensure_that_on_restart_future_completes` test case to validate
that the daemon destructor waits for the future completion and also delivers
the pending signals.

Also suppressed a few mock naggings.

Signed-off-by: Mustafa Kemal Gilor <mustafa.gilor@canonical.com>
AsyncPeriodicDownloadTask also carries a QFuture/QFutureWatcher pair.
Hence, update_manifests_all_task object also needs to be properly
shutdown before the daemon destructor exits.

Signed-off-by: Mustafa Kemal Gilor <mustafa.gilor@canonical.com>
Signed-off-by: Mustafa Kemal Gilor <mustafa.gilor@canonical.com>
@xmkg xmkg force-pushed the bugfix/ensure-daemon-waits-for-outstanding-futures branch from 0802da0 to 90099bd Compare April 9, 2025 15:01
.WillOnce(Return(mp::VirtualMachine::State::stopped));
EXPECT_CALL(*mock_vm, start).Times(1);

mpt::Signal signal;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice interplay of the mpt::Signal in the main thread and the side thread.

@georgeliao
Copy link
Contributor

georgeliao commented Apr 10, 2025

@xmkg
Good work, the timer.stop() + waitForFinished() + QCoreApplication::processEvents(QEventLoop::AllEvents); in the destructor looks idiomatic QT to me.

@xmkg
Copy link
Member Author

xmkg commented Apr 10, 2025

@georgeliao thanks for the review!

Right now, I'm also looking into moving the future cleanup calls into their respective types/classes, but that could be handled in a separate PR -- maybe in the "handle cleanup properly when the daemon constructor throws" PR. Meanwhile, I'm happy to land this one.

@levkropp this one's all yours now. Let me know if you want me to make any changes or provide an explanation.

Copy link
Contributor

@levkropp levkropp left a comment

Choose a reason for hiding this comment

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

LGTM! Testing on my local machine showed no regressions to the daemon functionality or test suite with these changes

@xmkg xmkg added this pull request to the merge queue Apr 10, 2025
@xmkg
Copy link
Member Author

xmkg commented Apr 10, 2025

@georgeliao @levkropp thanks folks!

Merged via the queue into main with commit 0b1f216 Apr 10, 2025
15 checks passed
@xmkg xmkg deleted the bugfix/ensure-daemon-waits-for-outstanding-futures branch April 10, 2025 14:59
@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