Use Task.start_link in stream worker for caller propagation#11
Open
MikaAK wants to merge 1 commit into
Open
Conversation
f2b3be3 to
ceb38c1
Compare
MikaAK
added a commit
to MikaAK/lang_ex
that referenced
this pull request
Apr 23, 2026
… + Task.start_link caller propagation (freshaengineering#11 upstream)
The stream worker was spawn_link'd without propagating the caller's process dictionary, so libraries relying on per-PID ownership (e.g. Ecto.Adapters.SQL.Sandbox.allow/3, custom per-test sandboxes keyed by $callers, or process-dictionary-based stubs) could not see the stream consumer as a caller. Switch spawn_link to Task.start_link: Task internally captures and restores $callers and $ancestors, gets OTP-style crash logging for free, and participates correctly in telemetry/tracing trees. No manual Process.put dance and no behaviour change for callers that did not rely on ownership propagation. Includes a regression test asserting the worker sees the consumer PID in both $callers and $ancestors.
ceb38c1 to
92a3818
Compare
Contributor
|
@claude review |
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.
Summary
LangEx.Graph.Stream.start_execution/3spawns its worker viaspawn_link/1, which does not propagate the parent's$callers/$ancestorsprocess-dictionary entries. This breaks libraries that rely on per-PID ownership to route calls back to the test process, including:Ecto.Adapters.SQL.Sandbox.allow/3/Mode.SandboxOwnerwhen the worker touches a shared DB connection$callers)Today the stream worker is invisible to these mechanisms, so tests that stub LLM responses per-PID silently fall through to the real HTTP layer (or fail with "no stub registered") when the code under test flows through
Graphs.stream/3.The fix
Swap
spawn_link/1forTask.start_link/1.Taskalready captures and restores$callers/$ancestorsinternally (that is whatTask.Supervised.start_linkdoes under the hood), and brings a few additional wins for free:Process.put(:"$callers", ...)danceRegression test
Added in
test/lang_ex/graph/stream_test.exs: invokes the stream, has a node send itsProcess.get(:"$callers")/Process.get(:"$ancestors")back to the consumer, and asserts the consumer PID appears in both lists.Compatibility
Task.start_linklinks to the calling process just likespawn_linkTesting