Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

fix: streaming responses lock-up under some conditions #105

Merged
merged 3 commits into from
Jan 4, 2024

Conversation

rustatian
Copy link
Member

@rustatian rustatian commented Jan 4, 2024

Reason for This PR

closes: roadrunner-server/roadrunner#1830

Description of Changes

  • Fix some states repeated.
  • Use enough buffer size to handle even huge streams.
  • General refactoring.

License Acceptance

By submitting this pull request, I confirm that my contribution is made under
the terms of the MIT license.

PR Checklist

[Author TODO: Meet these criteria.]
[Reviewer TODO: Verify that these criteria are met. Request changes if not]

  • All commits in this PR are signed (git commit -s).
  • The reason for this PR is clearly provided (issue no. or explanation).
  • The description of changes is clear and encompassing.
  • Any required documentation changes (code and docs) are included in this PR.
  • Any user-facing changes are mentioned in CHANGELOG.md.
  • All added/changed functionality is tested.

Summary by CodeRabbit

  • Bug Fixes

    • Improved worker state transitions and error handling for more robust operation.
    • Enhanced stream processing logic for better performance and reliability.
  • Refactor

    • Split stream iteration methods to support context passing, allowing for more controlled execution.
    • Refactored execution methods for clarity and maintainability.
  • Tests

    • Updated test cases to reflect changes in worker and pool behavior.
    • Introduced delays in tests to accommodate new timing constraints.
  • Performance Improvements

    • Increased buffer size for response channels to reduce potential bottlenecks.

Signed-off-by: Valery Piashchynski <piashchynski.valery@gmail.com>
Signed-off-by: Valery Piashchynski <piashchynski.valery@gmail.com>
Signed-off-by: Valery Piashchynski <piashchynski.valery@gmail.com>
@rustatian rustatian added B-bug Bug: bug, exception B-regression Bug: regression bugs labels Jan 4, 2024
@rustatian rustatian requested a review from wolfy-j January 4, 2024 19:50
@rustatian rustatian self-assigned this Jan 4, 2024
Copy link

coderabbitai bot commented Jan 4, 2024

Walkthrough

The overarching changes involve enhancing the robustness and responsiveness of the worker pool and stream processing within a concurrent execution environment. This includes handling early client disconnections, improving state management, and increasing buffer sizes to prevent lock-ups. The adjustments aim to ensure that workers recover gracefully from interrupted streams and that the system can handle high volumes of data without stalling.

Changes

File Path Change Summary
ipc/pipe/.../pipe_factory_spawn_test.go, ipc/pipe/.../pipe_factory_test.go Added state transitions post-execution in test functions.
pool/static_pool/debug.go Increased resp channel buffer size and updated stream iteration to support context.
pool/static_pool/supervisor_test.go Introduced delays in test functions for worker supervision.
pool/static_pool/workers_pool.go Refined worker state transitions, error handling, and stream processing.
worker/worker.go Split stream iteration methods, updated stream cancellation, refactored exec method, and added sendPONG method.

Assessment against linked issues

Objective Addressed Explanation
Prevent worker lock-up due to early client disconnections (#1830)
Ensure worker recovery without manual intervention (#1830)
Handle large response chunks without causing lock-ups (#1830)
Maintain system responsiveness under high data loads (#1830)

Related issues

  • No additional related issues identified as the provided information and changes are specific to the objectives of the linked issue #1830.

Poem

In the burrow of code, where the data streams flow,
The CodeRabbit hopped, with fixes in tow.
No more shall the workers in deadlock be caught,
For with wisdom and care, a solution was wrought. 🐇💻✨

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit's AI:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid.
    • @coderabbitai read the files in the src/scheduler package and generate README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@rustatian rustatian changed the title Fix/stream exec ttl fix: streaming responses lock-up under some conditions Jan 4, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 15

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5417ebb and d41504f.
Files ignored due to filter (2)
  • go.mod
  • go.sum
Files selected for processing (6)
  • ipc/pipe/pipe_factory_spawn_test.go (1 hunks)
  • ipc/pipe/pipe_factory_test.go (1 hunks)
  • pool/static_pool/debug.go (2 hunks)
  • pool/static_pool/supervisor_test.go (2 hunks)
  • pool/static_pool/workers_pool.go (4 hunks)
  • worker/worker.go (7 hunks)
Files skipped from review due to trivial changes (1)
  • pool/static_pool/supervisor_test.go
Additional comments: 4
pool/static_pool/debug.go (2)
  • 74-74: Replacing StreamIter with StreamIterWithContext is a good practice as it allows for better control over stream iteration, especially in cases where early termination might be needed. This change aligns with the PR's goal to handle early client disconnections more gracefully.

  • 76-77: Using runtime.Goexit() to terminate the goroutine after sending an error on the resp channel is appropriate here. It ensures that the goroutine exits cleanly without affecting other running goroutines.

ipc/pipe/pipe_factory_spawn_test.go (1)
  • 399-399: Adding w.State().Transition(fsm.StateReady) after w.Exec calls ensures that the worker state is reset to Ready after each execution. This reflects the expected state transitions in the actual worker lifecycle and should lead to more accurate testing.
ipc/pipe/pipe_factory_test.go (1)
  • 424-424: Including w.State().Transition(fsm.StateReady) after each w.Exec call in the test ensures that the worker's state is consistent with the expected behavior after task execution. This change improves the test's fidelity to real-world scenarios.

pool/static_pool/debug.go Show resolved Hide resolved
pool/static_pool/workers_pool.go Show resolved Hide resolved
pool/static_pool/workers_pool.go Show resolved Hide resolved
pool/static_pool/workers_pool.go Show resolved Hide resolved
pool/static_pool/workers_pool.go Show resolved Hide resolved
worker/worker.go Show resolved Hide resolved
worker/worker.go Show resolved Hide resolved
Comment on lines +308 to +310
if !w.State().Compare(fsm.StateWorking) {
return errors.Errorf("worker is not in the Working state, actual state: (%s)", w.State().String())
}
Copy link

Choose a reason for hiding this comment

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

The state check in StreamCancel (lines 308-310) is good practice, but it should also ensure that the worker is transitioned to an appropriate state after cancellation.

+ // TODO: Verify that the worker state is correctly managed after a stream cancellation.

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if !w.State().Compare(fsm.StateWorking) {
return errors.Errorf("worker is not in the Working state, actual state: (%s)", w.State().String())
}
// TODO: Verify that the worker state is correctly managed after a stream cancellation.
if !w.State().Compare(fsm.StateWorking) {
return errors.Errorf("worker is not in the Working state, actual state: (%s)", w.State().String())
}

Comment on lines +386 to 390

c := w.getCh()
// set last used time
w.State().SetLastUsed(uint64(time.Now().UnixNano()))
w.State().Transition(fsm.StateWorking)
Copy link

Choose a reason for hiding this comment

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

The Exec method (lines 386-390) should have a clear strategy for handling errors that occur during the execution of a payload, including cleanup and state management.

+ // TODO: Define a clear error handling strategy for the Exec method.

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
c := w.getCh()
// set last used time
w.State().SetLastUsed(uint64(time.Now().UnixNano()))
w.State().Transition(fsm.StateWorking)
c := w.getCh()
// TODO: Define a clear error handling strategy for the Exec method.
// set last used time
w.State().SetLastUsed(uint64(time.Now().UnixNano()))
w.State().Transition(fsm.StateWorking)

worker/worker.go Show resolved Hide resolved
Copy link

codecov bot commented Jan 4, 2024

Codecov Report

Attention: 137 lines in your changes are missing coverage. Please review.

Comparison is base (5417ebb) 77.20% compared to head (d41504f) 73.61%.

Files Patch % Lines
worker/worker.go 6.41% 73 Missing ⚠️
pool/static_pool/workers_pool.go 24.39% 62 Missing ⚠️
pool/static_pool/debug.go 33.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #105      +/-   ##
==========================================
- Coverage   77.20%   73.61%   -3.59%     
==========================================
  Files          23       23              
  Lines        2088     2164      +76     
==========================================
- Hits         1612     1593      -19     
- Misses        421      519      +98     
+ Partials       55       52       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rustatian rustatian merged commit e3ef277 into master Jan 4, 2024
8 checks passed
@rustatian rustatian deleted the fix/stream-exec-ttl branch January 4, 2024 20:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
B-bug Bug: bug, exception B-regression Bug: regression bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[🐛 BUG]: Streaming responses can experience a lock-up when the client disconnects early
1 participant