Aias00 opened a new pull request, #3438:
URL: https://github.com/apache/dubbo-go/pull/3438

   ## Problem
   
   `ExchangeClient.init` is a plain `bool` field with a `// FIXME atomic 
operation` comment acknowledging the race. It is written in `doInit`/`Close` 
and read (indirectly via `doInit`) in `Request`/`AsyncRequest`/`Send` without 
any synchronization.
   
   **Race scenarios:**
   - Multiple goroutines concurrently call `Request`/`AsyncRequest`/`Send` → 
all enter `doInit` → all see `init == false` → `Connect` is called multiple 
times
   - `Close()` writes `init = false` concurrently with `doInit` reading/writing 
`init` → memory visibility issue
   
   ## Fix
   
   Replace `init bool` with `uatomic.Bool` (already imported in this file as 
`go.uber.org/atomic`), consistent with the existing `activeNum uatomic.Uint32` 
field in the same struct:
   
   | Change | Detail |
   |--------|--------|
   | `init bool` → `init uatomic.Bool` | Same package as `activeNum`, already 
imported |
   | `doInit` check-then-set → `CAS(false, true)` | Atomic compare-and-swap 
ensures only one goroutine performs `Connect` |
   | CAS failure → spin-wait | Goroutines that lose the CAS wait for init to 
complete instead of skipping or duplicating |
   | Connect failure → `Store(false)` | Reset flag on failure so future calls 
can retry (important behavior missing in original code) |
   | `Close` → `Store(false)` | Atomic write ensures visibility |
   
   This follows the same pattern used by `BaseInvoker` (`uatomic.Bool`), 
`BaseClusterInvoker` (`atomic.Bool` with CAS), and `Directory` (`atomic.Bool` 
with CAS + mutex) elsewhere in the codebase.
   
   ## Tests
   
   - **`TestExchangeClientConcurrentDoInit`**: 20 goroutines concurrently call 
`doInit` — verifies `Connect` is called exactly once
   - **`TestExchangeClientDoInitFailureResets`**: verifies failed `doInit` 
resets the init flag and retry succeeds
   - All existing tests pass with `-race` flag enabled
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to