Skip to content

Conversation

@cdillond
Copy link

@cdillond cdillond commented Oct 1, 2025

I noticed that the existing code probably didn't do quite what was intended. In its current state, it immediately overwrites the "more" variable received from the tokChan with the result of the type assertion. That seems like a bug. Additionally, the entire pattern that it uses is unnecessary and error prone: select statements are non-deterministic, so the default statement block might execute even if the channel is closed or not empty. I changed the code so that it checks the length of the channel before receiving. This way, the receive is guaranteed not to block and it will always execute whenever the channel is not empty.

…tended. In its current state, it immediately overwrites the "more" variable received from the tokChan with the result of the type assertion. That seems like a bug. Additionally, the entire pattern that it uses is unnecessary and error prone: select statements are non-deterministic, so the default statement block might execute even if the channel is closed or not empty. I changed the code so that it checks the length of the channel before receiving. This way, the receive is guaranteed not to block and it will always execute whenever the channel is not empty.
@codecov-commenter
Copy link

codecov-commenter commented Oct 1, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.15%. Comparing base (8c35947) to head (3f84904).

Additional details and impacted files
@@ Coverage Diff @@ ## main #292 +/- ## ========================================== - Coverage 75.32% 75.15% -0.17%  ========================================== Files 33 33 Lines 6500 6504 +4 ========================================== - Hits 4896 4888 -8  - Misses 1321 1330 +9  - Partials 283 286 +3 

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
@shueybubbles shueybubbles requested a review from Copilot October 1, 2025 17:48
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug in the token processing logic and improves the channel receive pattern. The existing code had a variable shadowing issue where the more variable from the channel receive was immediately overwritten by the type assertion result, and used a potentially non-deterministic select statement pattern.

  • Fixed variable shadowing bug in token channel processing
  • Replaced select-with-default pattern with length check for deterministic behavior
  • Simplified the non-blocking channel receive logic
token.go Outdated
case tok, more := <-t.tokChan:
err, more := tok.(error)
if more {
if len(t.tokChan) > 0 {
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

Using len() to check if a channel receive will succeed is not reliable in concurrent code. The channel length can change between the len() check and the subsequent receive operation if another goroutine receives from the channel. This creates a race condition that could cause the goroutine to block indefinitely. The original select statement with default case was the correct approach for non-blocking receives, though the variable naming issue should be fixed differently.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

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

Oops - I guess I was wrong about how default cases in select statements work. Sorry!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants