wait until processImpl completes before closing stream chan #1677
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix race condition in query processing
Problem
There was a race condition between closing the
stream
channel and writing to it from a background goroutine inconn_query.go
.Race condition scenario:
conn_query.go
starts a goroutine that callsc.process(ctx, onProcess)
(line 45)c.process()
spawns another goroutine internally that callsc.processImpl()
(line 106 inconn_process.go
)c.processImpl()
reads data from the server and callson.data(block)
for each data block (line 170 inconn_process.go
)c.process()
returns withctx.Err()
conn_query.go
immediately closes thestream
channel (line 54)stream
viaon.data(block)
panic: send on closed channel
Stack trace example:
Solution
Modified
conn_process.go
to ensure that theprocess()
function waits for the internal goroutine to complete before returning, even when the context is cancelled.Changes:
ctx.Done()
is triggered, after callingc.cancel()
, we now wait for the goroutine to finish by selecting onerrCh
ordoneCh
on.data
will not be called afterprocess()
returnsstream
channel can now be safely closed inconn_query.go
without race conditionsImpact