Skip to content
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

fix: adopt install uninstall chan to resolve deadlock on subscription #81

Merged
merged 4 commits into from
Oct 24, 2024

Conversation

beer-1
Copy link
Collaborator

@beer-1 beer-1 commented Oct 24, 2024

Description

Closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Enhanced subscription management with new channels for installation and uninstallation notifications.
    • Improved event handling through the renamed eventLoop method, allowing dynamic subscription management.
  • Bug Fixes

    • Added safety checks to ensure filters exist before deletion, enhancing reliability during uninstallation.
  • Refactor

    • Updated subscription creation logic to improve lifecycle management and prevent deadlocks.
    • Refactored the clearSubscription method for better handling of subscription uninstallation.

@beer-1 beer-1 self-assigned this Oct 24, 2024
@beer-1 beer-1 requested a review from a team as a code owner October 24, 2024 04:25
Copy link

coderabbitai bot commented Oct 24, 2024

Warning

Rate limit exceeded

@beer-1 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 5 minutes and 2 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Files that changed from the base of the PR and between fc19f1a and dda53fc.

Walkthrough

The changes introduce enhancements to the FilterAPI and subscription structures within the jsonrpc/namespaces/eth/filters package. New channels for subscription management are added, and the subscribeEvents method is renamed to eventLoop, which now handles subscription installation and uninstallation. The subscription struct is updated with an id field and additional channels to signal installation and errors. The uninstallation process is refined to prevent deadlocks, improving thread safety and subscription lifecycle management.

Changes

File Path Change Summary
jsonrpc/namespaces/eth/filters/api.go - Added channels: install, uninstall in FilterAPI struct.
- Renamed method: subscribeEvents to eventLoop.
- Updated locking mechanism for thread safety.
- Modified UninstallFilter to check filter existence.
jsonrpc/namespaces/eth/filters/subscriptions.go - Added field: id in subscription struct.
- Added channels: installed, err, and field unsubOnce in subscription struct.
- Updated methods to handle new id and subscription signaling.
- Refactored clearSubscription method to accept a subscription pointer.

Possibly related PRs

Poem

In the meadow where filters play,
New channels hop and dance today.
With subscriptions bright and clear,
Installation brings us cheer!
Uninstall with care, oh so neat,
Thread safety makes our work complete! 🐇✨


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

@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: 2

🧹 Outside diff range and nitpick comments (4)
jsonrpc/namespaces/eth/filters/subscriptions.go (1)

185-207: Excellent deadlock prevention mechanism!

The implementation effectively prevents deadlocks by:

  1. Using sync.Once for atomic uninstallation
  2. Draining pending events while attempting uninstallation
  3. Waiting for confirmation of cleanup completion

Consider enhancing the comments to explicitly explain the deadlock prevention mechanism:

-			// write uninstall request and consume logs/hashes. This prevents
-			// the eventLoop broadcast method to deadlock when writing to the
-			// filter event channel while the subscription loop is waiting for
-			// this method to return (and thus not reading these events).
+			// Prevent deadlocks by draining any pending events while attempting
+			// to write the uninstall request. This ensures that:
+			// 1. The eventLoop can continue broadcasting without blocking
+			// 2. No events are lost before uninstallation
+			// 3. The subscription loop can safely exit without deadlocking
jsonrpc/namespaces/eth/filters/api.go (3)

74-76: Add context-based cancellation for goroutines.

The eventLoop goroutine lacks a cancellation mechanism. Consider adding a context to FilterAPI for graceful shutdown of all goroutines.

type FilterAPI struct {
+	ctx    context.Context
+	cancel context.CancelFunc
	...
}

func NewFilterAPI(app *app.MinitiaApp, backend *backend.JSONRPCBackend, logger log.Logger) *FilterAPI {
+	ctx, cancel := context.WithCancel(context.Background())
	api := &FilterAPI{
+		ctx:    ctx,
+		cancel: cancel,
		...
	}
	...
-	go api.eventLoop()
+	go api.eventLoop(ctx)
	return api
}

Also applies to: 84-84


Line range hint 105-174: Improve concurrency handling in eventLoop.

Several concurrency-related issues need attention:

  1. Lock granularity: The current implementation holds locks while appending to slices, which could block other operations unnecessarily.
  2. Subscription management: The installation process lacks error handling and could lead to deadlocks if the subscription is invalid.
  3. Potential race condition: Events might be processed before the subscription is fully installed.

Consider these improvements:

func (api *FilterAPI) eventLoop() {
+	defer api.cancel() // Ensure cleanup on exit
	for {
		select {
+		case <-api.ctx.Done():
+			return
		case s := <-api.install:
+			if err := api.validateSubscription(s); err != nil {
+				s.err <- err
+				close(s.err)
+				close(s.installed)
+				continue
+			}
			api.subscriptions[s.id] = s
			close(s.installed)
		case s := <-api.uninstall:
+			if s == nil {
+				continue
+			}
			delete(api.subscriptions, s.id)
			close(s.err)
		case block := <-api.blockChan:
			api.filtersMut.Lock()
+			filters := make(map[rpc.ID]*filter, len(api.filters))
+			for id, f := range api.filters {
+				filters[id] = f
+			}
			api.filtersMut.Unlock()
			
-			for _, f := range api.filters {
+			for _, f := range filters {
				if f.ty == ethfilters.BlocksSubscription {
+					api.filtersMut.Lock()
					f.hashes = append(f.hashes, block.Hash())
+					api.filtersMut.Unlock()
				}
			}
		}
	}
}

+func (api *FilterAPI) validateSubscription(s *subscription) error {
+	if s == nil {
+		return errors.New("nil subscription")
+	}
+	// Add more validation as needed
+	return nil
+}

Based on the gathered information, I can now provide a final response:

Concurrent testing is indeed needed for subscription management

The codebase shows several concurrent patterns that require testing:

  • Mutex-protected shared state (filtersMut sync.Mutex)
  • Multiple channels for communication (install, uninstall, headerChan, logsChan, etc.)
  • Goroutines handling subscriptions (seen in subscription handlers)
  • No existing test files found in the filters package

Specific areas that need test coverage:

  • Race conditions between filter mutations and event processing
  • Channel communication patterns in eventLoop
  • Subscription installation and uninstallation concurrency
  • Cleanup of resources in error scenarios
🔗 Analysis chain

Line range hint 105-174: Add concurrent testing for subscription management.

The new subscription management logic would benefit from comprehensive concurrent testing to verify the absence of deadlocks and race conditions.

Would you like me to help generate concurrent test cases for the subscription management functionality?


Let me gather more information about the testing coverage for this functionality.

This will help us understand the current test coverage and concurrent testing patterns in the filters package.


Let me check for more details about the existing test coverage and synchronization mechanisms.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for existing concurrent tests
rg -l "testing\.T.*goroutine.*subscription" 

# Look for potential race conditions
rg "go run.*race" 

# Check for existing benchmark tests
rg "testing\.B.*subscription"

Length of output: 97


Script:

#!/bin/bash
# Check for any test files related to filters
fd ".*test.*" jsonrpc/namespaces/eth/filters/

# Look for existing test patterns in filter tests
rg -A 5 "func Test.*Filter" jsonrpc/namespaces/eth/filters/

# Check for concurrent operations in filter tests
rg -l "sync\." jsonrpc/namespaces/eth/filters/

# Look for existing subscription tests
rg -A 5 "func.*subscription" jsonrpc/namespaces/eth/filters/

Length of output: 841


Script:

#!/bin/bash
# Check the content of api.go for mutex usage
rg -A 3 "sync\.Mutex|sync\.RWMutex" jsonrpc/namespaces/eth/filters/api.go

# Look for test files specifically
fd ".*_test\.go$" jsonrpc/namespaces/eth/filters/

# Check for any existing test setup with channels
rg -A 5 "make\(chan" jsonrpc/namespaces/eth/filters/

# Look for any race detection comments or directives
rg -i "race|concurrent" jsonrpc/namespaces/eth/filters/

Length of output: 3856

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c76e767 and 4e6060f.

📒 Files selected for processing (2)
  • jsonrpc/namespaces/eth/filters/api.go (9 hunks)
  • jsonrpc/namespaces/eth/filters/subscriptions.go (7 hunks)
🔇 Additional comments (5)
jsonrpc/namespaces/eth/filters/subscriptions.go (4)

Line range hint 16-29: Well-designed subscription struct enhancements!

The new fields provide robust synchronization mechanisms:

  • id for unique identification
  • installed channel for installation signaling
  • err channel for uninstallation signaling
  • unsubOnce for atomic uninstallation

This design effectively prevents race conditions in the subscription lifecycle.


45-54: Robust subscription initialization and cleanup pattern!

The changes implement a thread-safe subscription lifecycle:

  1. Creates subscription with proper initialization
  2. Signals installation via channel
  3. Waits for confirmation before proceeding
  4. Uses subscription-based cleanup

This pattern effectively prevents race conditions during subscription setup and teardown.

Also applies to: 57-57


107-118: Consistent implementation of subscription pattern!

The changes correctly implement the same thread-safe pattern as NewHeads while preserving the existing filtering logic.

Also applies to: 121-121


154-165: Consistent implementation of subscription pattern!

The changes correctly implement the same thread-safe pattern while maintaining the flexibility to handle both full transactions and hashes.

Also applies to: 168-168

jsonrpc/namespaces/eth/filters/api.go (1)

55-58: 🛠️ Refactor suggestion

Consider using buffered channels for subscription management.

The newly added channels install and uninstall are unbuffered, which could potentially block the sender in high-concurrency scenarios. Consider using buffered channels to reduce contention.

-	install   chan *subscription // install filter for event notification
-	uninstall chan *subscription // remove filter for event notification
+	install   chan *subscription // install filter for event notification
+	uninstall chan *subscription // remove filter for event notification
+	const subscriptionBufferSize = 16 // adjust based on expected concurrent subscriptions

Likely invalid or redundant comment.

jsonrpc/namespaces/eth/filters/api.go Show resolved Hide resolved
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.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
jsonrpc/namespaces/eth/filters/api.go (2)

55-58: Enhance channel documentation for clarity.

While the comments describe the basic purpose, consider adding more detailed documentation about:

  • The expected message flow through these channels
  • Whether these channels are buffered/unbuffered
  • Who is responsible for sending/receiving on these channels
 // Channels for subscription managements
-	install   chan *subscription // install filter for event notification
-	uninstall chan *subscription // remove filter for event notification
+	// install receives new subscriptions from clients and signals completion
+	// through the subscription's installed channel
+	install chan *subscription
+
+	// uninstall receives terminated subscriptions and signals completion
+	// through the subscription's err channel
+	uninstall chan *subscription

Line range hint 105-174: Review lock handling and error propagation in eventLoop.

The current implementation has several areas that could be improved:

  1. Lock granularity could be optimized to reduce contention
  2. Missing timeout handling for channel operations
  3. Limited error handling for subscription operations

Consider these improvements:

 func (api *FilterAPI) eventLoop() {
+	// Add context for graceful shutdown
+	ctx, cancel := context.WithCancel(context.Background())
+	defer cancel()
+
 	for {
 		select {
 		case block := <-api.blockChan:
-			api.filtersMut.Lock()
-			for _, f := range api.filters {
+			// Copy filters under lock to minimize lock duration
+			api.filtersMut.Lock()
+			filters := make(map[rpc.ID]*filter, len(api.filters))
+			for id, f := range api.filters {
+				filters[id] = f
+			}
+			api.filtersMut.Unlock()
+
+			// Process without holding lock
+			for _, f := range filters {
 				if f.ty == ethfilters.BlocksSubscription {
 					f.hashes = append(f.hashes, block.Hash())
 				}
 			}
-			api.filtersMut.Unlock()
+
+			// Update under lock
+			api.filtersMut.Lock()
+			for id, f := range filters {
+				if existing, ok := api.filters[id]; ok {
+					existing.hashes = f.hashes
+				}
+			}
+			api.filtersMut.Unlock()

 			// Similar pattern for logs and pending transactions...

 		case s := <-api.install:
+			select {
+			case <-ctx.Done():
+				s.err <- errors.New("shutdown in progress")
+				close(s.err)
+				return
+			default:
+			}
+
 			api.filtersMut.Lock()
 			api.subscriptions[s.id] = s
 			api.filtersMut.Unlock()
 			close(s.installed)

 		case s := <-api.uninstall:
+			select {
+			case <-ctx.Done():
+				return
+			default:
+			}
+
 			api.filtersMut.Lock()
 			delete(api.subscriptions, s.id)
 			api.filtersMut.Unlock()
 			close(s.err)
+		case <-ctx.Done():
+			return
 		}
 	}
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 4e6060f and fc19f1a.

📒 Files selected for processing (1)
  • jsonrpc/namespaces/eth/filters/api.go (9 hunks)
🔇 Additional comments (1)
jsonrpc/namespaces/eth/filters/api.go (1)

305-307: Reference existing optimization suggestion.

The current implementation could benefit from the lock optimization suggestions provided in the previous review.

Copy link

codecov bot commented Oct 24, 2024

Codecov Report

Attention: Patch coverage is 3.12500% with 62 lines in your changes missing coverage. Please review.

Project coverage is 28.06%. Comparing base (c76e767) to head (dda53fc).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
jsonrpc/namespaces/eth/filters/subscriptions.go 0.00% 40 Missing ⚠️
jsonrpc/namespaces/eth/filters/api.go 0.00% 21 Missing ⚠️
jsonrpc/backend/eth.go 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #81      +/-   ##
==========================================
- Coverage   28.14%   28.06%   -0.08%     
==========================================
  Files         124      124              
  Lines       13638    13676      +38     
==========================================
  Hits         3838     3838              
- Misses       9239     9277      +38     
  Partials      561      561              
Files with missing lines Coverage Δ
x/evm/precompiles/cosmos/contract.go 49.56% <100.00%> (ø)
jsonrpc/backend/eth.go 0.00% <0.00%> (ø)
jsonrpc/namespaces/eth/filters/api.go 0.00% <0.00%> (ø)
jsonrpc/namespaces/eth/filters/subscriptions.go 0.00% <0.00%> (ø)

@beer-1 beer-1 merged commit 875632b into main Oct 24, 2024
6 checks passed
@beer-1 beer-1 deleted the fix/subscription-deadlock branch October 24, 2024 04:53
@coderabbitai coderabbitai bot mentioned this pull request Oct 24, 2024
11 tasks
@coderabbitai coderabbitai bot mentioned this pull request Nov 27, 2024
11 tasks
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