fix(future): race-safe remote_future_dispose — sever producer under the mutex before closing the trigger (true-async/server#93)#182
Merged
Conversation
…er the shared-state mutex before closing the trigger (true-async/server#93) Disposing a remote future while its producer thread is still completing it raced: remote_future_dispose closed the cross-thread trigger and dropped the state ref without first marking the state completed or detaching target_future, so a concurrent async_future_shared_state_complete() could ring a closing handle or resolve a freed future. It now mirrors async_future_state_object_free — under the state mutex it grabs the trigger, NULLs it, sets completed=1 and clears target_future, then disposes the trigger outside the lock — so a late completion is a clean no-op. This lets an owner dispose an unresolved remote future safely (e.g. the HTTP server reaping per-worker completion futures on pool shutdown).
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
remote_future_dispose()closed the shared-state's cross-thread trigger and dropped the state ref without first marking the state completed or detachingtarget_future. A producer thread completing the future at the same moment (async_future_shared_state_complete/reject) could then:uv_closed, ortarget_futurethat the owner just freed.So disposing an unresolved remote future was unsafe — only safe once it had already resolved.
Fix
Mirror
async_future_state_object_free(): under the shared-state mutex, grab the trigger, NULL it, setcompleted = 1and cleartarget_future, then dispose the trigger outside the lock. A concurrentcomplete()/reject()(both gated oncompletedunder the same mutex) becomes a clean no-op.This lets an owner dispose an unresolved remote future safely — e.g.
HttpServerreaping the per-worker completion futures returned bysubmit_internal()on pool shutdown (true-async/server#93), which otherwise left their triggers armed on the parent reactor (loop-alive assert on debug / leaked libuv handle on release).Test
Covered end-to-end by the server suite (graceful_shutdown + reload → shutdown); no ABI change.