Skip to content

[Brook K12] Added Producer scopes for Activity attempts and clients. - #222

Closed
moetemp wants to merge 1 commit into
moe/brook/review/streams-k11-static-callsfrom
moe/brook/review/streams-k12-scopes
Closed

moetemp wants to merge 1 commit into
moe/brook/review/streams-k11-static-callsfrom
moe/brook/review/streams-k12-scopes

Conversation

@moetemp

@moetemp moetemp commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

What changed?

This PR adds Producer scopes. A scope is the Producer of one Activity attempt or one client, with one writer for each Stream, so lang keeps only a scope id.

  • OpenActivityScope derives the Producer from the attempt's ActivityContext. From attempt 2, it opens a writer on each Stream in the attempt's arguments, so the attempt withdraws the earlier attempts' Records even if it writes nothing.
  • OpenClientScope is client:<identity>:<key> at epoch 1, with a key from the runtime's random key and a counter. With a name it is client:<name> at the given epoch. A forked Python child builds a new runtime, so it gets a new key with no fork hook.
  • Publish opens the scope's writer on first use and writes. CloseStreamAs closes that writer and then the Stream, as the scope's Producer. An Activity scope refuses it with NOT_OWNER and calls no Store.
  • CloseScope closes the writers and reports a failed one in its answer. FreeScope drops a scope with no wait, for a finalizer.
  • A dropped OpenActivityScope or CloseScope releases the writers that the scope holds.
  • The writer table counts lang's handles and the scopes' handles apart. CloseWriters and Shutdown release lang's only, since another Worker of the client may still use a scope's writer, and a scope releases its own when it ends. A late FreeWriter from lang takes no scope handle.
  • Shutdown closes one scope at a time. A dropped one puts back the scope that it was closing, and the next Shutdown ends it. While it runs, the Free methods still work, so lang can end a buffer whose pump is gone. After it, only Shutdown, the static methods and the Free methods run, and the Free methods do nothing. A scope or writer that opens during it is freed and refused.
  • A writer's retry duration counts as at most a day, so the retry math can't overflow.
  • CloseStreamAs holds the Stream's open lock while it ends the writer, so a concurrent first publish can't share that writer.
  • Flaky, the fault-injecting memory Store of the writer suite, moves to the suites' support module for the scope tests.
  • The scope suite runs through the byte call, as lang sends it.

Depends on #221 (K11). Review lane is the Rust reviewers.
Part of the Brook review series.

Why?

The Activity interceptors of sdk-python, and later of TypeScript and .NET, then fill one ActivityContext and keep one id. The Producer rule, the attempt-start withdrawal and the writer table live here once.

How did you test it?

cargo fmt, cargo clippy -- -D warnings (with and without features), cargo test -p temporalio-streams --all-features and cargo doc pass at this commit. The scope suite runs on the memory Store here, and on Redis in R4 (temporal-community/sdk-streams#4).

@moetemp moetemp added the skip-changelog Changelog entry rides another PR label Oct 28, 2026
@moetemp
moetemp force-pushed the moe/brook/review/streams-k12-scopes branch from eb14f46 to 061873d Compare October 28, 2026 18:45
@moetemp
moetemp marked this pull request as draft October 28, 2026 21:55
@moetemp
moetemp force-pushed the moe/brook/review/streams-k12-scopes branch from 061873d to 55b88d8 Compare October 28, 2026 22:42
@moetemp
moetemp force-pushed the moe/brook/review/streams-k11-static-calls branch from 6306039 to aa401f9 Compare October 29, 2026 04:06
@moetemp
moetemp force-pushed the moe/brook/review/streams-k12-scopes branch from 55b88d8 to b98e0f1 Compare October 29, 2026 04:06
@moetemp
moetemp force-pushed the moe/brook/review/streams-k11-static-calls branch from aa401f9 to df49f36 Compare October 29, 2026 08:37
@moetemp
moetemp force-pushed the moe/brook/review/streams-k12-scopes branch from b98e0f1 to 3ccfc8c Compare October 29, 2026 08:37
@moetemp
moetemp force-pushed the moe/brook/review/streams-k11-static-calls branch from df49f36 to cc13519 Compare October 29, 2026 09:17
@moetemp
moetemp force-pushed the moe/brook/review/streams-k12-scopes branch from 3ccfc8c to 8d224d8 Compare October 29, 2026 09:17
A scope keeps the Producer of one Activity attempt or client and its writers, so lang keeps only an id. `Shutdown` closes every scope and writer for the backend shutdown.
@moetemp
moetemp force-pushed the moe/brook/review/streams-k11-static-calls branch from cc13519 to ce11ed4 Compare October 29, 2026 10:03
@moetemp
moetemp force-pushed the moe/brook/review/streams-k12-scopes branch from 8d224d8 to 70943ca Compare October 29, 2026 10:03
@moetemp

moetemp commented Oct 30, 2026

Copy link
Copy Markdown
Owner Author

Parked. Nothing calls these runtime methods yet, since sdk-python does this work in Python. The branch stays, and this layer comes back with its first caller, for example a TypeScript or .NET port or Rust read handles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-changelog Changelog entry rides another PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant