Skip to content

fix(api): subscribe before namespace workflow dispatch#4654

Open
NathanFlurry wants to merge 1 commit intographite-base/4654from
break-up/subscribe-before-namespace-dispatch
Open

fix(api): subscribe before namespace workflow dispatch#4654
NathanFlurry wants to merge 1 commit intographite-base/4654from
break-up/subscribe-before-namespace-dispatch

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 14, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@claude
Copy link
Copy Markdown

claude bot commented Apr 14, 2026

Code Review

PR: fix(api): subscribe before namespace workflow dispatch
Status: Draft | +9/-9 lines


Overview

This PR fixes a classic race condition in the namespace creation API (engine/packages/api-peer/src/namespaces.rs). Previously, the workflow was dispatched before subscriptions were established, meaning if the workflow completed (or failed) very quickly, the events would be emitted before any subscriber was listening — causing the API handler to hang indefinitely waiting for events that already fired.

The fix reorders operations to: subscribe → dispatch → wait, which is the correct pattern for event-driven pub-sub systems.


Analysis

Correctness: This fix is clearly right.

The original ordering:

  1. Dispatch workflow
  2. Subscribe to CreateComplete
  3. Subscribe to Failed
  4. Wait

The fixed ordering:

  1. Subscribe to CreateComplete
  2. Subscribe to Failed
  3. Dispatch workflow
  4. Wait

The subscribe-before-publish pattern is fundamental: the listener must be in place before triggering the event source. This is especially important because namespace creation could complete near-synchronously in local/test environments with minimal I/O.


Observations

  • Minimal and focused — no unrelated changes, exactly the right scope.
  • No timeout on tokio::select! — if neither CreateComplete nor Failed ever fires (e.g., the workflow panics without signaling), the API handler will wait indefinitely. This is pre-existing behavior not introduced here, but worth noting as a follow-up.
  • Subscription cleanup on dispatch failure — if dispatch() fails, create_sub and fail_sub are dropped without use. This is safe as long as the subscription type handles deregistration on drop, which appears to be the case.
  • Pattern audit — any other workflow dispatch + subscription patterns in the API layer should be audited for the same subscribe-after-dispatch bug.

Suggestions

  1. Follow-up: add a deadline/timeout to the tokio::select! to prevent indefinite hangs if the workflow silently fails to emit a completion signal.
  2. Follow-up: audit other callsites with the dispatch-then-subscribe pattern across the engine API layer to ensure this isn't repeated.

Summary

This is a correct and necessary bug fix. The change is small, targeted, and applies the right subscribe-before-dispatch ordering. Good to merge once out of draft.

@NathanFlurry NathanFlurry changed the base branch from 04-14-chore_engine_publish_engine_bases_in_ci to graphite-base/4654 April 14, 2026 23:43
@NathanFlurry NathanFlurry marked this pull request as ready for review April 15, 2026 01:09
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.

1 participant