Skip to content

fail any rpc call which blocks the runServer loop for more than 1s #1861

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

Merged
merged 6 commits into from
Jan 28, 2025

Conversation

sawka
Copy link
Member

@sawka sawka commented Jan 28, 2025

No description provided.

Copy link
Contributor

coderabbitai bot commented Jan 28, 2025

Walkthrough

The pull request introduces a series of modifications across multiple files in the project, focusing on enhancing the RPC (Remote Procedure Call) system and adding new functionality. The changes primarily involve updating function signatures to include additional parameters, such as debug names and client identifiers, across various utility and client-related files. A new ExpMap data structure is introduced in the pkg/util/ds/expmap.go file, providing a generic map with key expiration capabilities. The wshremote package gains a new StreamTestCommand method for streaming integer responses. The modifications aim to improve debugging, client identification, and add more flexible communication mechanisms within the system, while maintaining the existing core functionality and error handling strategies.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@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.

Actionable comments posted: 1

🔭 Outside diff range comments (1)
go.mod (1)

Line range hint 3-3: Fix invalid Go version.

The specified Go version 1.23.4 is invalid as Go versions only go up to 1.22.x currently. This will prevent builds from working correctly.

Apply this diff to fix the Go version:

-go 1.23.4
+go 1.22.1
🧹 Nitpick comments (5)
pkg/util/ds/expmap.go (3)

15-19: Use a read-write mutex or separate locks for heap vs. map for potential high concurrency scenarios.
While a single sync.Mutex is simpler, if the map will be accessed heavily in parallel, consider a read-write mutex or separate locks for the heap and map storage for improved concurrency performance.


42-48: Add optional background cleanup scheduler.
The current approach only expires items on demand (during Get). Consider adding a background goroutine or scheduling mechanism to remove expired entries proactively, preventing the map from growing too large.


50-58: Revisit duplicate entries scenario.
Inserting new entries in the heap without removing old ones may lead to unnecessary memory usage. Maintaining a secondary index or removing duplicates could be beneficial in memory-constrained environments.

cmd/wsh/cmd/wshcmd-test.go (1)

10-16: Extend usage documentation.
This hidden command may need more explanation or a “Long” description for users who discover it. Even hidden dev commands benefit from clarity on their purpose.

pkg/wshrpc/wshremote/wshremote.go (1)

48-62: Consider enhancing the test command.

While the StreamTestCommand is useful for testing, consider:

  1. Adding configurable delay between messages to simulate slow processing
  2. Adding configurable message count and buffer size
  3. Adding documentation about its testing purpose

This would make it more versatile for testing RPC blocking scenarios.

 func (impl *ServerImpl) StreamTestCommand(ctx context.Context) chan wshrpc.RespOrErrorUnion[int] {
-	ch := make(chan wshrpc.RespOrErrorUnion[int], 16)
+	const (
+		defaultBufferSize = 16
+		defaultCount     = 1000
+		defaultDelay     = 0 * time.Millisecond
+	)
+	ch := make(chan wshrpc.RespOrErrorUnion[int], defaultBufferSize)
 	go func() {
 		defer close(ch)
 		idx := 0
 		for {
+			select {
+			case <-ctx.Done():
+				return
+			default:
+			}
 			ch <- wshrpc.RespOrErrorUnion[int]{Response: idx}
 			idx++
-			if idx == 1000 {
+			if idx == defaultCount {
 				break
 			}
+			time.Sleep(defaultDelay)
 		}
 	}()
 	return ch
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 02e4bdc and 430939f.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (13)
  • cmd/server/main-server.go (1 hunks)
  • cmd/wsh/cmd/wshcmd-connserver.go (1 hunks)
  • cmd/wsh/cmd/wshcmd-root.go (3 hunks)
  • cmd/wsh/cmd/wshcmd-test.go (1 hunks)
  • go.mod (1 hunks)
  • pkg/util/ds/expmap.go (1 hunks)
  • pkg/waveapp/waveapp.go (1 hunks)
  • pkg/wshrpc/wshclient/barerpcclient.go (1 hunks)
  • pkg/wshrpc/wshremote/wshremote.go (1 hunks)
  • pkg/wshrpc/wshserver/wshserverutil.go (1 hunks)
  • pkg/wshutil/wshrouter.go (0 hunks)
  • pkg/wshutil/wshrpc.go (9 hunks)
  • pkg/wshutil/wshutil.go (6 hunks)
💤 Files with no reviewable changes (1)
  • pkg/wshutil/wshrouter.go
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: CodeQL
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Analyze (go)
  • GitHub Check: Build for TestDriver.ai
🔇 Additional comments (18)
pkg/util/ds/expmap.go (2)

31-40: Consider making the comparator function type-aware.
Using any casting might risk runtime panics if an unexpected type is ever passed in. A dedicated, typed comparator (or a safe runtime type-check) can help prevent accidental misuse.


60-79: Validate performance for large heaps.
For certain use cases, repeatedly calling expireItems_nolock() could lead to performance bottlenecks when many items are near expiration. More sophisticated data structures or a background sweeper might improve performance.

cmd/wsh/cmd/wshcmd-test.go (1)

18-20: Verify command integration with rootCmd.
Double-check that testCmd does not conflict with other hidden commands and that rootCmd properly handles top-level args when test is invoked.

pkg/wshrpc/wshserver/wshserverutil.go (1)

26-26: Confirm the “main-client” debugName usage.
Ensure that any downstream debugging or logging facility consistently uses "main-client" to identify this RPC client and continue verifying that calls exceeding 1s are properly handled or terminated, per the PR objective.

pkg/wshrpc/wshclient/barerpcclient.go (1)

34-34: LGTM! Debug name addition aids in RPC timeout tracking.

The addition of the debug name "bare-client" will help identify which RPC client is causing timeouts in the runServer loop, aligning with the PR's objective.

cmd/wsh/cmd/wshcmd-root.go (1)

89-89: LGTM! Consistent debug name additions for RPC tracking.

The addition of descriptive debug names ("wshcmd-termclient" and "wshcmd") across all RPC client setup functions will help identify the source of any RPC calls that exceed the 1-second timeout limit.

Also applies to: 151-151, 169-169

cmd/wsh/cmd/wshcmd-connserver.go (1)

140-140: LGTM! Using route ID as debug name enables precise timeout tracking.

Using authRtn.RouteId as the debug name is an excellent choice as it provides a unique identifier for each connection, making it easier to track down which specific connection is causing RPC timeouts.

cmd/server/main-server.go (2)

199-199: LGTM! Debug name addition for local connection tracking.

The addition of the "conn:local" debug name helps identify RPC calls from the local connection client.


199-201: Verify timeout handling implementation.

While debug names have been added to track RPC calls, the actual timeout handling mechanism to fail calls that block for more than 1s is not visible in these changes. Please ensure that the timeout handling is implemented elsewhere.

Run the following script to locate the timeout implementation:

✅ Verification successful

Timeout handling is properly implemented

The timeout handling for RPC calls is implemented in pkg/wshutil/wshrpc.go:

  • Blocking calls are tracked with 1s expiration
  • Context timeout of 1s is enforced for RPC handling
  • Response channel has timeout protection
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for timeout-related changes in the RPC implementation

# Look for timeout-related code in RPC handling
rg -A 5 'func.*RunServer|func.*HandleRpc|timeout.*1.*s|time\.Second'

# Look for changes to RPC context or options
ast-grep --pattern 'type RpcOpts struct {
  $$$
  Timeout $_
  $$$
}'

Length of output: 29857

pkg/wshutil/wshrpc.go (3)

30-31: LGTM! Good use of expiring map for tracking blocking responses.

Using ds.ExpMap with expiring entries is a good approach to prevent log spam when multiple responses are blocked.


186-189: LGTM! Enhanced RPC data structure.

Adding Command and Route fields to rpcData improves error reporting by providing more context when RPC calls block.


756-783: Well-implemented blocking detection with timeout.

The implementation properly handles blocking RPC responses:

  1. Attempts non-blocking send first
  2. Logs blocking occurrence (with rate limiting via expMap)
  3. Retries with 1s timeout
  4. Fails the RPC call if still blocked after timeout

This aligns well with the PR objective of failing blocked RPC calls.

pkg/wshutil/wshutil.go (4)

202-206: LGTM! Enhanced debugging for terminal RPC setup.

Adding debugStr parameter helps identify which RPC client is experiencing blocking issues.


224-228: LGTM! Consistent debug naming in packet RPC setup.

The debug name parameter is consistently propagated to the underlying RPC client.


Line range hint 241-263: LGTM! Debug support for connection RPC setup.

The connection RPC setup properly handles the debug name parameter.


Line range hint 275-284: LGTM! Debug support for domain socket RPC.

The domain socket RPC setup correctly propagates the debug name.

pkg/waveapp/waveapp.go (1)

179-179: LGTM! Client properly identifies itself in RPC setup.

Using "vdomclient" as the debug identifier helps track RPC blocking issues from the VDOM client.

go.mod (1)

12-12: Verify the usage of the gods library.

The github.com/emirpasic/gods library has been added, presumably to support the RPC timeout functionality. Let's verify its usage in the codebase.

✅ Verification successful

The gods library is appropriately used for implementing an expiring map data structure

The library is used in pkg/util/ds/expmap.go to implement a thread-safe map with automatic key expiration using a binary heap for efficient timeout tracking. This is a well-designed utility that could support various features including RPC timeout management.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check how the gods library is being used in the codebase
# Expected: Find imports and usage of gods library in relation to RPC timeouts

# Search for gods imports
echo "=== Searching for gods imports ==="
rg --type go "github.com/emirpasic/gods"

# Search for specific data structures from gods that might be used for RPC tracking
echo -e "\n=== Searching for potential gods data structures usage ==="
rg --type go "gods\.(TreeMap|HashMap|Set|List)"

# Look for new files that might use the gods library
echo -e "\n=== Searching for recently modified files that might use gods ==="
git diff --name-only | xargs rg --type go "gods\."

Length of output: 570


Script:

#!/bin/bash
# Check the implementation and usage of expmap
echo "=== Content of expmap.go ==="
cat pkg/util/ds/expmap.go

echo -e "\n=== Files referencing expmap ==="
rg --type go "expmap"

echo -e "\n=== RPC timeout related code ==="
rg --type go -i "rpc.*timeout|timeout.*rpc"

Length of output: 10205

Comment on lines +22 to +24
func runTestCmd(cmd *cobra.Command, args []string) error {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Implement the actual test logic or remove placeholder code.
Returning nil suggests no operation performed. If the command is intended for testing system functionality, consider adding meaningful actions or removing the command to avoid confusion.

@sawka sawka merged commit 5418e5a into main Jan 28, 2025
7 of 8 checks passed
@sawka sawka deleted the sawka/rpc-debug branch January 28, 2025 06:38
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