fix(flatpak): replace detached threads with promise-tracked workers#243
Open
aki1770-del wants to merge 2 commits into
Open
fix(flatpak): replace detached threads with promise-tracked workers#243aki1770-del wants to merge 2 commits into
aki1770-del wants to merge 2 commits into
Conversation
Five std::thread().detach() sites in FlatpakShim had no lifetime tracking. If FlatpakShim is destroyed while a transaction is in flight, the destructor returns without waiting for running threads, leaving strand_, event sinks, and the operation tracker accessible to the still-running thread. Adds TrackWorker(std::future<void>) and WorkerGuard RAII struct. The destructor waits on all pending futures before releasing members. WorkerGuard ensures promise resolution on every exit path, including the multiple early-returns in ApplicationInstall. Per toyota-connected#231, the two hardcoded sleep_for(seconds(2)) TOCTOU races in ApplicationInstall are also replaced. A GFile monitor was evaluated but requires async integration with the existing strand executor that is out of scope here; bounded polling (10 x 500ms, 5s max) provides deterministic behavior consistent with the strand model. GFile monitor integration can follow as a separate PR. Fixes toyota-connected#231 Signed-off-by: Akihiro Komada <aki1770@gmail.com>
Fix 4 Chromium-style violations flagged by CI format job: - Single-statement if bodies moved to indented next line (3 sites) - Long string literal split across lines (1 site) Co-Authored-By: Claude and aki1770-del <aki1770@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Five
std::thread().detach()sites inFlatpakShimhave no lifetimetracking. If
FlatpakShimis destroyed while a flatpak transaction isin flight, the destructor returns immediately — leaving
strand_,event sinks, and the operation tracker accessible to the still-running
thread. A use-after-free in the Flatpak plugin host process crashes
the IVI HMI process entirely.
Additionally,
ApplicationInstallcontains twosleep_for(seconds(2))calls used as a proxy for filesystem sync — a TOCTOU race that fails
on slow systems and wastes time on fast ones.
Fixes #231
Changes
Thread lifetime tracking
Added
TrackWorker(std::future<void>)toFlatpakShim, implementingthe
std::future/std::promisepattern recommended in #231:TrackWorkerstores a future per spawned thread, pruning completedfutures on each call via a non-blocking
wait_for(seconds(0))probeWorkerGuardRAII struct (anonymous namespace in.cc) ensurespromise->set_value()is called on every exit path, including thesix early-return branches in
ApplicationInstallApplicationInstallflatpak-installApplicationUninstallflatpak-uninstallApplicationUpdateflatpak-updateinstall_runtimeflatpak-runtimeinstall_extensionsflatpak-extTOCTOU sleep replacement (
ApplicationInstallonly)Both
sleep_for(seconds(2)) + one-shot checkblocks are replacedwith bounded poll loops (10 × 500 ms, 5 s max) that exit as soon as
flatpak_installation_get_installed_refreturns a valid ref.A
GFileMonitorimplementation was evaluated per #231's recommendation.It requires additional async integration with the existing strand executor;
bounded polling provides deterministic behavior consistent with the strand
model and is reviewable in this PR's scope. GFile monitor integration can
follow as a separate PR if preferred.
Fix Pattern
Verification
asio::postcallbacks, and error returns are preserved exactlyWorkerGuardcallsset_value()after the thread body exits —wait()in the destructor correctly indicates thread completionTrackWorker'swait_for(seconds(0))probe never blocks thecalling thread
case exits earlier; existing check logic is structurally identical
Part of the security audit series: #227 #228 #229 #232 #233 #234 #235 #236
Signed-off-by: aki1770-del aki1770@gmail.com