Skip to content

feat(rivetkit): expose c.keepAwake(promise) through native path#4748

Merged
NathanFlurry merged 1 commit intomainfrom
sleep-cleanup/07-keep-awake-native-api
Apr 27, 2026
Merged

feat(rivetkit): expose c.keepAwake(promise) through native path#4748
NathanFlurry merged 1 commit intomainfrom
sleep-cleanup/07-keep-awake-native-api

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 24, 2026

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

@claude
Copy link
Copy Markdown

claude Bot commented Apr 24, 2026

Code Review: feat(rivetkit): expose c.keepAwake(promise) through native path

Overview

This PR replaces the keepAwake implementation on the native path from using registerTask (NAPI-side task tracking channel) to using the core keep_awake API (increments the sleep-blocking counter directly in rivetkit-core). This is a meaningful semantic improvement: the old path only tracked the task for shutdown ordering, while the new path correctly blocks both idle sleep and grace finalize until the promise settles.


Issues

Missing rejection logging in NAPI keep_awake

The new NAPI method passes the promise directly to core without handling rejections:

pub async fn keep_awake(
    &self,
    promise: Promise<serde_json::Value>,
) -> napi::Result<serde_json::Value> {
    self.inner.keep_awake(promise).await
}

Both sibling methods log when their promise rejects:

  • wait_untiltracing::warn!(?error, "actor wait_until promise rejected")
  • register_tasktracing::warn!(?error, "actor keep_awake promise rejected")

The new keep_awake silently drops rejections (the void callNative(...) on the TypeScript side swallows the resulting NAPI error). If the original promise rejects, the keep-awake guard is properly decremented (RAII _guard drop), but the rejection goes unlogged. Consider adding the same warning pattern:

pub async fn keep_awake(
    &self,
    promise: Promise<serde_json::Value>,
) -> napi::Result<()> {
    self.inner.keep_awake(async move {
        if let Err(error) = promise.await {
            tracing::warn!(?error, "actor keep_awake promise rejected");
        }
    }).await;
    Ok(())
}

This also simplifies the return type to () since the caller ignores the value anyway.


Redundant Promise.resolve() wrapper

this.#ctx.keepAwake(Promise.resolve(promise).then(() => null)),

promise is already a Promise<T>, so Promise.resolve(promise) is a no-op. Can be simplified to:

this.#ctx.keepAwake(promise.then(() => null)),

Silent error discard vs. the old targeted swallow

The old code swallowed only isClosedTaskRegistrationError (task registration closed/not configured during teardown) while letting other errors surface. The new code uses void callNative(...) which silently swallows all errors from the native call itself (distinct from the promise rejection). This is probably fine since core's keep_awake doesn't use a closeable channel, but it is a broader suppression than what CLAUDE.md recommends ("Swallow only that specific bridge error"). A comment noting the trade-off, or a targeted error filter, would make the intent explicit.


No driver tests for the semantic change

The core keep_awake tests (e.g., sleep_shutdown_waits_for_keep_awake_work_then_finishes_next_tick in rivetkit-core/tests/modules/task.rs) cover the Rust side, but there are no TypeScript driver tests verifying that the native keepAwake path actually blocks idle sleep. Given that the old implementation silently failed to block sleep (it only used register_task), a driver-level test would be valuable to prevent regression.


Positive notes

  • Clean simplification: the old path was more complex (sync call, error type check, detached tracking). The new path is shorter and the semantics match the documented intent.
  • The _guard in core's keep_awake is RAII, so the counter is correctly decremented on both success and error/panic paths.
  • Comment in native.ts clearly explains the non-obvious detail that core only observes the settle signal, not the value.
  • The index.d.ts type declaration correctly reflects the new async signature.

@NathanFlurry NathanFlurry force-pushed the sleep-cleanup/06-ts-deprecate-prevent-sleep branch from 6966cdb to 8650b44 Compare April 24, 2026 11:48
@NathanFlurry NathanFlurry force-pushed the sleep-cleanup/07-keep-awake-native-api branch 2 times, most recently from 571bece to ef299f2 Compare April 24, 2026 12:14
@NathanFlurry NathanFlurry force-pushed the sleep-cleanup/06-ts-deprecate-prevent-sleep branch from 31f3a57 to dfa956c Compare April 24, 2026 12:32
@NathanFlurry NathanFlurry force-pushed the sleep-cleanup/07-keep-awake-native-api branch from ef299f2 to b27b338 Compare April 24, 2026 12:32
@NathanFlurry NathanFlurry force-pushed the sleep-cleanup/06-ts-deprecate-prevent-sleep branch from dfa956c to 181bb0f Compare April 24, 2026 13:16
@NathanFlurry NathanFlurry force-pushed the sleep-cleanup/07-keep-awake-native-api branch from b27b338 to 0454dc5 Compare April 24, 2026 13:16
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 24, 2026

Preview packages published to npm

Install with:

npm install rivetkit@pr-4748

All packages published as 0.0.0-pr.4748.ee4cd1d with tag pr-4748.

Engine binary is shipped via @rivetkit/engine-cli on linux-x64-musl, linux-arm64-musl, darwin-x64, and darwin-arm64. Windows users should use the release installer or set RIVET_ENGINE_BINARY.

Docker images:

docker pull rivetdev/engine:slim-ee4cd1d
docker pull rivetdev/engine:full-ee4cd1d
Individual packages
npm install rivetkit@pr-4748
npm install @rivetkit/react@pr-4748
npm install @rivetkit/rivetkit-napi@pr-4748
npm install @rivetkit/workflow-engine@pr-4748

Base automatically changed from sleep-cleanup/06-ts-deprecate-prevent-sleep to main April 27, 2026 07:13
@NathanFlurry NathanFlurry merged commit 3ee5e43 into main Apr 27, 2026
38 of 52 checks passed
@NathanFlurry NathanFlurry deleted the sleep-cleanup/07-keep-awake-native-api branch April 27, 2026 07:14
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