-
-
Notifications
You must be signed in to change notification settings - Fork 638
Refactor generateRSCPayload from global to parameter #2061
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
Refactor generateRSCPayload from global to parameter #2061
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ Finishing touches🧪 Generate unit tests (beta)
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. Comment |
Code Review - PR #2061: Refactor generateRSCPayload from global to parameterSummaryThis PR addresses a critical data leakage and race condition issue by converting ✅ Strengths1. Excellent Problem Identification
2. Clean Architecture
3. Type Safety
4. Good Test Coverage
5. Documentation
🔍 Potential Issues & Suggestions1. Error Handling Edge Case (Minor)Location: if (!this.generateRSCPayload) {
throw new Error(
'generateRSCPayload is not defined. Please ensure that you are using at least version 4.0.0 of ' +
'React on Rails Pro and the Node renderer, and that ReactOnRailsPro.configuration.enable_rsc_support ' +
'is set to true.',
);
}Issue: The error message still references version requirements and configuration, but the actual issue could now be:
Suggestion: Consider making the error message more specific: if (!this.generateRSCPayload) {
throw new Error(
'generateRSCPayload function is not available. This could mean: ' +
'(1) The function was not passed to RSCRequestTracker constructor, ' +
'(2) ReactOnRailsPro.configuration.enable_rsc_support is not enabled, or ' +
'(3) You are using an incompatible version of React on Rails Pro (requires 4.0.0+).'
);
}2. Undefined Check Pattern (Minor)Location: generateRSCPayload: typeof generateRSCPayload !== 'undefined' ? generateRSCPayload : undefined,Observation: This is a safe pattern, but since Suggestion: Consider adding a comment explaining when this would be undefined: # generateRSCPayload is undefined when rendering on the RSC bundle (see line 22-26)
generateRSCPayload: typeof generateRSCPayload !== 'undefined' ? generateRSCPayload : undefined,3. Type Consistency (Very Minor)Location: The 4. Potential Memory Leak Prevention (Question)Location: The Question: Should the clear(): void {
// ... existing stream cleanup ...
this.generateRSCPayload = undefined; // Help GC
}This is likely premature optimization, but worth considering if memory profiling shows issues. 🛡️ Security Considerations✅ No new security concerns introduced
⚡ Performance Considerations✅ Performance impact: Negligible to positive
🧪 Test Coverage AssessmentCurrent Coverage: Good ✅
Potential Gaps:
Recommendation: Add a test that verifies request isolation: it('isolates generateRSCPayload between concurrent requests', async () => {
// Spawn two requests with different generateRSCPayload implementations
// Verify each request uses its own function, not the other's
});📋 Code Quality✅ Follows Best Practices
✅ Follows Repository Conventions
🎯 Recommendations SummaryRequired Changes: None ❌This PR is ready to merge as-is. Suggested Improvements (Optional):
Final Verdict: ✅ APPROVEThis is a high-quality refactoring that:
The changes are well-thought-out and the implementation is clean. The optional suggestions above are minor improvements that could be addressed in follow-up PRs if desired. Great work! 🎉 Review conducted using repository's CLAUDE.md guidelines |
Code Review: Refactor generateRSCPayload from global to parameterSummaryThis PR successfully addresses a critical data leakage and race condition issue by converting ✅ Strengths1. Excellent Problem Identification & Solution
2. Strong Type Safety
3. Backward Compatibility
4. Test Coverage Improvements
5. Clean Architecture
🔍 Potential Issues & Suggestions1. Error Message Clarity (Minor - Suggestion)In 2. Test Config Duplication (Minor - Observation)In 3. Missing Type Import Location Check (Minor)In 4. Ruby String Interpolation Edge Case (Low Priority)In 🔒 Security Considerations✅ No Issues Found
⚡ Performance Considerations✅ Positive Impact
📝 Test Coverage Assessment✅ Excellent Coverage
Suggested Additional Tests (Optional)
🎯 Code Quality✅ Follows Best Practices
🎉 Overall AssessmentRating: ⭐⭐⭐⭐⭐ Excellent This is a high-quality refactoring that:
The fact that a previously skipped test now passes is strong evidence the fix works as intended. Recommendation: ✅ APPROVE - Ready to merge after confirming lint checks pass. 📋 Checklist
Reviewed by: Claude Code |
d9c77fa to
6364abd
Compare
Fix data leakage issue by passing generateRSCPayload as a parameter instead of defining it as a global function in the VM context. Changes: - Changed globalThis.generateRSCPayload to local const in server_rendering_js_code.rb - Pass generateRSCPayload as parameter to ReactOnRails rendering functions - Update RSCRequestTracker to accept generateRSCPayload in constructor - Update streamingUtils to extract and pass generateRSCPayload from options - Add GenerateRSCPayloadFunction type and add to Params interface - Update test fixtures to reflect new local function pattern This prevents concurrent requests from sharing state and eliminates potential race conditions where one request could use another's context. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Add verification that the handleRequestClosed sink method is properly called when HTTP connections close in incremental render tests. Changes: - Update createMockSink() to return handleRequestClosed mock - Update createBasicTestSetup() and createStreamingTestSetup() helpers - Add handleRequestClosed verification to 5 tests - Use waitFor() to handle async cleanup timing All 11 tests pass, confirming handleRequestClosed is called once per connection close across both mocked and streaming scenarios. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Prevent parallel test execution conflicts by giving each test file its
own unique bundle directory instead of sharing a common directory.
Changes:
- Add createTestConfig() function that generates unique bundle paths
- Update incrementalHtmlStreaming.test.ts to use unique path
- Update htmlStreaming.test.js to use unique path
- Update concurrentHtmlStreaming.test.ts to use unique path
- Keep backward compatibility with default config
Each test now uses: tmp/node-renderer-bundles-test-{testName}
Previously all tests shared: tmp/node-renderer-bundles-test
This eliminates race conditions when tests run in parallel, preventing
intermittent failures from concurrent access to the same directory.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
…e parallel requests
c31292f to
0d5c62f
Compare
Code Review: Refactor generateRSCPayload from global to parameterSummaryThis PR addresses a critical concurrency issue by eliminating global state in the RSC payload generation system. The refactoring is well-architected and significantly improves request isolation. ✅ Strengths1. Excellent Problem Identification & Solution 🎯
2. Type Safety 💪Clear type definition added to types/index.ts with proper integration into Params interface and optional parameter handling with undefined checks. 3. Comprehensive Test Coverage ✅
4. Clean Implementation 🏗️
5. Backwards Compatibility 🔄
Code Quality ObservationsPositive:
Minor notes:
Security & Performance✅ Security: No concerns - removing global state improves security by preventing data leakage Potential Issues & Suggestions1. Cleanup verification (Question)Is RSCRequestTracker.clear() called explicitly in finally blocks, or does it rely on GC? Explicit cleanup is safer for long-running processes. 2. Test cleanup (Minor)Consider adding afterAll hooks to clean up tmp directories created by createTestConfig to prevent bloat during development. 3. Stricter validation (Enhancement)Could add early validation in streamingUtils.ts when extracting generateRSCPayload from options, though current lazy validation is acceptable. Testing RecommendationsAlready covered: ✅ Concurrent requests, data isolation, error cases Optional additions:
Changelog & Documentation✅ REQUIRED: Update /react_on_rails_pro/CHANGELOG.md per CLAUDE.md guidelines This is user-visible because it fixes critical data leakage in RSC with streaming. Suggested entry: #### Bug Fixes
- [PR 2061](https://github.com/shakacode/react_on_rails/pull/2061) by [AbanoubGhadban](https://github.com/AbanoubGhadban): Fixed critical data leakage issue in React Server Components where concurrent requests could share state due to global generateRSCPayload function. Refactored to use request-scoped parameters for proper isolation.Final Verdict✅ APPROVE with minor suggestions This is high-quality work that:
Before merge:
Great work! 🎉 |
9b1430c
into
abanoubghadban/pro509/make-renderer-use-ndjson-for-communication
Summary
Fix data leakage issue by passing
generateRSCPayloadas a parameter instead of defining it as a global function in the VM context.Problem
Currently,
generateRSCPayloadis defined asglobalThis.generateRSCPayloadin the VM, which causes:Solution
Create
generateRSCPayloadas a local function inside the IIFE, then pass it as a parameter to the server rendering functions. This ensures:Changes
globalThis.generateRSCPayloadto localconstinserver_rendering_js_code.rbgenerateRSCPayloadas parameter to ReactOnRails rendering functionsRSCRequestTrackerto acceptgenerateRSCPayloadin constructorstreamingUtilsto extract and passgenerateRSCPayloadfrom optionsGenerateRSCPayloadFunctiontype and add toParamsinterfaceFiles Changed
lib/react_on_rails_pro/server_rendering_js_code.rbpackages/react-on-rails/src/types/index.tspackages/react-on-rails-pro/src/RSCRequestTracker.tspackages/react-on-rails-pro/src/streamingUtils.tspackages/node-renderer/tests/fixtures/projects/spec-dummy/asyncComponentsTreeForTestingRenderingRequest.jspackages/node-renderer/tests/incrementalHtmlStreaming.test.tsTests
Impact
This prevents concurrent requests from sharing state and eliminates potential race conditions where one request could use another's context.
🤖 Generated with Claude Code