Skip to content

Fix yield, shield, and checkCanceled to work from main thread - #183

Merged
lalinsky merged 1 commit into
mainfrom
fix-yield-from-main
Jan 8, 2026
Merged

lalinsky merged 1 commit into
mainfrom
fix-yield-from-main

Conversation

@lalinsky

@lalinsky lalinsky commented Jan 8, 2026

Copy link
Copy Markdown
Owner

Summary

  • Fix rt.yield() to actually run tasks when called from main (was returning immediately)
  • Fix beginShield, endShield, and checkCanceled to work from main thread context
  • Add tests for yield and sleep from main

Test plan

  • ./check.sh passes (213 tests)

Previously rt.yield() returned immediately when called from main,
preventing tasks from running. Now yield, beginShield, endShield,
and checkCanceled all work correctly from the main thread context.
@coderabbitai

coderabbitai Bot commented Jan 8, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Modified cooperative yielding to exclude main task queuing, refactored shield and cancellation logic to use a unified getCurrentTask() helper, and expanded yield/shield/pin APIs to function outside coroutine contexts. Docstrings updated to reflect broader usage. Added tests validating main-thread yielding.

Changes

Cohort / File(s) Summary
Yield and task scheduling
src/runtime.zig (Executor.yield)
Main task now skipped during cooperative yield by checking both desired_state and not is_main, reducing unnecessary scheduling overhead.
Shield and cancellation refactoring
src/runtime.zig (beginShield, endShield, checkCanceled)
Replaced direct current_coroutine lookups with getCurrentTask() helper; removed coroutine-context assertions, enabling invocation from main and non-coroutine contexts.
Pinning API generalization
src/runtime.zig (beginPin, endPin)
Dropped coroutine-context guards, allowing pinning operations to be called more broadly outside strict coroutine contexts.
Documentation and tests
src/runtime.zig (docstrings, test cases)
Updated docstrings for yield and shield methods to document main-thread and coroutine usage; added test cases for yielding from main and sleep-from-main scenarios.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

Main thread's no longer second-class—
Yield and shield now break through glass,
Coroutine chains no longer bind,
Cross-context flow, less overhead in mind. 🚀

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and clearly describes the main changes: fixing yield, shield, and checkCanceled to work from the main thread.
Description check ✅ Passed The description is directly related to the changeset, detailing the specific fixes and providing a test plan that validates the changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-yield-from-main

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6c4d298 and a88e07d.

📒 Files selected for processing (1)
  • src/runtime.zig
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: cubic · AI code reviewer
  • GitHub Check: test (windows-latest)
🔇 Additional comments (6)
src/runtime.zig (6)

492-494: Solid fix for main thread cooperative yielding.

Preventing main_task from being queued makes sense - it's checked directly via state.load() in runSchedulerLoop (lines 573, 597) rather than being pulled from a queue like spawned tasks. The scheduleTask function at line 800 has corresponding logic to handle main_task specially.


608-625: Clean refactor to use getCurrentTask() helper.

Centralizing the "get current task" logic in one place rather than duplicating the current_coroutine check. The helper at lines 646-652 handles both coroutine and main-thread contexts uniformly.


631-644: Intentional asymmetry with shield functions - makes sense.

Pinning prevents cross-thread migration, which only applies to spawned coroutines. Main thread can't migrate, so early-return at runtime level (lines 1052, 1062) is appropriate. The unreachable in executor methods acts as an assertion that the runtime wrapper is used correctly.

Also applies to: 1049-1063


1002-1003: Docstrings updated to match the new behavior.

Clear about the dual context support (main thread or coroutine) and the no-op behavior when no executor is present.

Also applies to: 1017-1018, 1026-1027, 1069-1070


1341-1370: Good test coverage for the main fix.

The iteration limit at line 1362 is a sensible safeguard against hanging tests. The test validates the exact behavior this PR fixes: yield() from main thread actually runs spawned tasks instead of returning immediately.


1372-1401: Validates sleep behavior from main context.

Similar structure to the yield test above, which is fine - they're testing different code paths (yield vs sleep). Both confirm that yielding control from main actually runs scheduled coroutines.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

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