-
Notifications
You must be signed in to change notification settings - Fork 129
fix(rivetkit): prevent sleeping if there are any action hono requests, fixes inspector requests not resetiting sleep timer #3319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
3 Skipped Deployments
|
Code ReviewThanks for this PR! The changes improve the sleep timer logic by tracking Hono HTTP requests instead of raw fetch requests, which makes sense. However, I've identified one critical bug that needs to be fixed before merging. 🐛 Critical BugLocation: __endHonoHttpRequest() {
this.#activeHonoHttpRequests = this.#activeHonoHttpRequests; // ❌ BUG: This doesn't decrement!
if (this.#activeHonoHttpRequests < 0) {
// ...
}
}Issue: Line 1889 assigns the counter to itself instead of decrementing it. This means:
Fix: __endHonoHttpRequest() {
this.#activeHonoHttpRequests--; // ✅ Decrement the counter
if (this.#activeHonoHttpRequests < 0) {
// ...
}
}Or more defensively: __endHonoHttpRequest() {
this.#activeHonoHttpRequests = Math.max(0, this.#activeHonoHttpRequests - 1);
if (this.#activeHonoHttpRequests < 0) { // This check becomes redundant with Math.max
// ...
}
}✅ Positive Observations
💡 Minor Suggestions
🧪 Testing RecommendationsAfter fixing the bug, please test:
SummaryMust fix before merge: The decrement bug on line 1889. Once that's fixed, this PR will properly solve the issue of inspector requests not resetting the sleep timer and improve the overall sleep management logic. Review generated with Claude Code |
31cb2fa to
3842c8f
Compare
PR Review: Fix RivetKit Sleep Timer for Inspector RequestsSummaryThis PR fixes an issue where inspector requests (and other Hono-routed HTTP requests) were not properly resetting the actor sleep timer. The solution replaces tracking of raw fetch requests with tracking of Hono HTTP requests via middleware. Positive Aspects✅ Correct Problem Identification: The PR correctly identifies that the previous approach of tracking ✅ Clean Architecture: Using Hono middleware to track requests is the right approach - it's centralized and catches all HTTP requests uniformly. ✅ Defensive Programming: The ✅ Proper Synchronization: The change from ✅ Better Documentation: Improved comments and JSDoc for Issues & Concerns1. CRITICAL: Potential Race Condition in Middlewarerouter.use("*", async (c, next) => {
const actor = await actorDriver.loadActor(c.env.actorId);
actor.__beginHonoHttpRequest();
try {
await next();
} finally {
actor.__endHonoHttpRequest();
}
});Problem: If an exception is thrown in Impact: This could cause the counter to go negative (which is caught by your guard) or allow the actor to sleep prematurely. Recommendation: Restructure to ensure symmetry: router.use("*", async (c, next) => {
const actor = await actorDriver.loadActor(c.env.actorId);
actor.__beginHonoHttpRequest();
try {
await next();
} finally {
actor.__endHonoHttpRequest();
}
});Actually, looking at this more carefully - if 2. WebSocket Requests May Be MiscountedLooking at router.ts:135-188, WebSocket upgrade requests go through the Hono router initially: router.get(PATH_CONNECT_WEBSOCKET, async (c) => {
const upgradeWebSocket = runConfig.getUpgradeWebSocket?.();
// ...
});Problem: WebSocket upgrade requests will increment Current Mitigation: You have Recommendation: Add a comment explaining that WebSocket upgrades briefly increment the HTTP counter but are ultimately tracked separately via 3. Missing Error Handling in
|
fe12a10 to
6282237
Compare
3842c8f to
5fc0aa1
Compare
Pull Request ReviewSummaryThis PR fixes two critical bugs in the actor sleep mechanism:
Code Quality ✅Strengths:
Good practices observed:
Potential Issues 🔍1. Race condition in middleware (Minor) const actor = await actorDriver.loadActor(c.env.actorId);
actor.__beginHonoHttpRequest();
try {
await next();
} finally {
actor.__endHonoHttpRequest();
}If 2. Public-like method naming (Stylistic)
3. Removed error logging in _sleep() (Minor concern) this._sleep().catch((error) => {
this.#rLog.error({ msg: "error during sleep", error: stringifyError(error) });
});This was removed since Performance Considerations ✅
Security Concerns ✅No security issues identified. The changes are purely internal lifecycle management. Test Coverage
|
PR ReviewSummaryThis PR fixes a critical bug where inspector requests (and other Hono action requests) weren't resetting the sleep timer. The fix refactors the sleep prevention mechanism from tracking raw fetch requests to tracking Hono HTTP requests via middleware. ✅ Strengths
🐛 Potential IssuesCritical: Race Condition in Request Tracking// router.ts:82-90
router.use("*", async (c, next) => {
const actor = await actorDriver.loadActor(c.env.actorId);
actor.__beginHonoHttpRequest();
try {
await next();
} finally {
actor.__endHonoHttpRequest();
}
});Problem: If an exception is thrown in Recommendation: Add error handling for the actor load: router.use("*", async (c, next) => {
const actor = await actorDriver.loadActor(c.env.actorId);
if (!actor) {
throw new Error("Actor not found");
}
actor.__beginHonoHttpRequest();
try {
await next();
} finally {
actor.__endHonoHttpRequest();
}
});Minor: Inconsistent Error HandlingIn // Old code (removed):
this._sleep().catch((error) => {
this.#rLog.error({
msg: "error during sleep",
error: stringifyError(error),
});
});Now Recommendation: Add try-catch in the setImmediate(() => {
try {
sleep();
} catch (error) {
this.#rLog.error({
msg: "error starting sleep",
error: stringifyError(error),
});
}
});⚡ Performance Considerations
🔒 Security ConsiderationsNo significant security concerns. The changes are internal refactoring without new attack surfaces. 🧪 Test CoverageMissing: The PR doesn't add tests for the new behavior. Consider adding:
📋 Minor Suggestions
📝 SummaryOverall Assessment: ✅ Approve with Minor Suggestions The fix correctly addresses the root cause and improves code clarity. The main concerns are:
The refactoring is a net improvement to maintainability. Once the race condition is addressed, this is ready to merge. Files Reviewed: ✅ All 7 changed files |
6282237 to
d22a4df
Compare
5fc0aa1 to
91f44ec
Compare
Code ReviewSummaryThis PR fixes a critical bug in the actor sleep logic. Previously, the sleep timer was only being reset for raw fetch requests, but not for Hono-routed HTTP requests (including inspector requests). This meant that inspector requests and other action requests weren't preventing actors from sleeping, which could cause actors to sleep while still handling active requests. Changes Overview
Positive Aspects ✅
Potential Issues
|
Code ReviewThanks for this PR! This is a solid fix for preventing actors from sleeping during active HTTP requests. Here's my review: ✅ Strengths
🔍 Observations & Suggestions1. Potential Race Condition (Minor)In router.use("*", async (c, next) => {
const actor = await actorDriver.loadActor(c.env.actorId);
actor.__beginHonoHttpRequest();
try {
await next();
} finally {
actor.__endHonoHttpRequest();
}
});Current code assumes 2. Method Naming Convention (Nitpick)The double underscore prefix (
Current naming is fine, just a style consideration. 3. Documentation EnhancementThe comment on 4. Test CoverageI don't see test changes for the new behavior. Consider adding a test that:
This would prevent regression of the inspector bug. 🐛 Potential IssuesNone identified - The code looks solid! 🔒 SecurityNo security concerns identified. ⚡ PerformanceThe middleware adds minimal overhead (just incrementing/decrementing a counter), which is negligible. The change should actually improve performance by preventing unnecessary wake-from-sleep cycles when inspector requests come in. 📝 Additional Notes
SummaryRecommendation: ✅ Approve with minor suggestions The fix correctly addresses the root cause by tracking HTTP requests at the right abstraction level. The code quality is good with proper error handling and clear documentation. My suggestions are mostly nitpicks around defensive programming and testing. Great work on the improved API naming and simplified async handling! |
Merge activity
|
…, fixes inspector requests not resetiting sleep timer (#3319)
…, fixes inspector requests not resetiting sleep timer
d22a4df to
82a8b53
Compare
91f44ec to
40b7e52
Compare
|
Deployment failed with the following error: Learn More: https://vercel.com/docs/limits#rate-limits |
Code Review for PR #3319SummaryThis PR fixes an important bug where inspector requests were not resetting the sleep timer, potentially causing actors to sleep during active inspector sessions. The solution refactors the sleep prevention logic from tracking raw fetch requests to tracking HTTP requests through the Hono router middleware. Positive Changes
Issues & Concerns1. Race Condition in Middleware (Medium Priority)The middleware at router.use("*", async (c, next) => {
const actor = await actorDriver.loadActor(c.env.actorId);
actor.__beginHonoHttpRequest();
try {
await next();
} finally {
actor.__endHonoHttpRequest();
}
});Issue: If the request handler calls Suggestion: Consider if this is the intended behavior. If requests should prevent sleep until fully complete, this is correct. Document this behavior if intentional. 2. WebSocket Upgrade Handling (Low Priority)WebSocket upgrades start as HTTP requests but transition to WebSocket connections. The middleware will:
This seems correct, but verify that there's no gap between Location: 3. Inconsistent Error Removal (Minor)At Question: Is this intentional? The new middleware-based approach should handle it, but worth confirming that raw fetch handlers (if any exist outside the router) are covered. 4. Missing Test Coverage (Medium Priority)The PR doesn't add tests for:
Suggestion: Add integration tests to verify the fix works as intended. 5. Typo in Log Message (Minor)At this.#rLog.debug({
msg: "actor instance onSocketMessage", // Should be "onSocketClosed"
rivetRequestId,
// ...
});Note: This existed before this PR, but worth fixing. Performance Considerations
Security Considerations
Style & Conventions✅ Follows Rivet conventions:
Recommendations
VerdictApprove with minor suggestions. The core fix is sound and addresses the reported issue. The recommendations above are mostly about edge cases and test coverage improvements. 🤖 Generated with Claude Code |
…, fixes inspector requests not resetiting sleep timer (#3319)

No description provided.