You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by lo...@apache.org on 2019/12/23 23:55:47 UTC
[beam] branch lostluck-readRace created (now 9c1bc02)
This is an automated email from the ASF dual-hosted git repository.
lostluck pushed a change to branch lostluck-readRace
in repository https://gitbox.apache.org/repos/asf/beam.git.
at 9c1bc02 [GoSDK] Fix race on reading channel readErr.
This branch includes the following new commits:
new 9c1bc02 [GoSDK] Fix race on reading channel readErr.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[beam] 01/01: [GoSDK] Fix race on reading channel readErr.
Posted by lo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
lostluck pushed a commit to branch lostluck-readRace
in repository https://gitbox.apache.org/repos/asf/beam.git
commit 9c1bc0233903257b8261df21b7df8c3565c51079
Author: Robert Burke <lo...@users.noreply.github.com>
AuthorDate: Mon Dec 23 15:55:35 2019 -0800
[GoSDK] Fix race on reading channel readErr.
Close off a race condition where a closing DataChannel might have new readers created for it while it is failing, causing stuckness in the bundles.
In particular, the c.readErr must be interacted while c.mu is held.
Otherwise something like the following happens.
Given a channel C, and goroutines G1,G2.
1. G1 A request for a new reader on C arrives, checks C.readErr finds it null.
2. G2 An error occurs on reading. The lock is acquired, and C.readErr is set. Readers are closed. The channel is officially closed with A.forceRecreate, removing it from the DataManager cache.
3. G1 calls A.makeReader, and creates a new reader there.
There could be an arbitrary number of G1s.
---
sdks/go/pkg/beam/core/runtime/harness/datamgr.go | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/sdks/go/pkg/beam/core/runtime/harness/datamgr.go b/sdks/go/pkg/beam/core/runtime/harness/datamgr.go
index 7e9f7b1..7615356 100644
--- a/sdks/go/pkg/beam/core/runtime/harness/datamgr.go
+++ b/sdks/go/pkg/beam/core/runtime/harness/datamgr.go
@@ -150,14 +150,14 @@ type DataChannel struct {
writers map[clientID]*dataWriter
readers map[clientID]*dataReader
-
// readErr indicates a client.Recv error and is used to prevent new readers.
readErr error
+
// a closure that forces the data manager to recreate this stream.
forceRecreate func(id string, err error)
cancelFn context.CancelFunc // Allows writers to stop the grpc reading goroutine.
- mu sync.Mutex // guards both the readers and writers maps.
+ mu sync.Mutex // guards mutable internal data, notably the maps and readErr.
}
func newDataChannel(ctx context.Context, port exec.Port) (*DataChannel, error) {
@@ -198,6 +198,8 @@ func (c *DataChannel) terminateStreamOnError(err error) {
// OpenRead returns an io.ReadCloser of the data elements for the given instruction and ptransform.
func (c *DataChannel) OpenRead(ctx context.Context, ptransformID string, instID instructionID) io.ReadCloser {
+ c.mu.Lock()
+ defer c.mu.Unlock()
cid := clientID{ptransformID: ptransformID, instID: instID}
if c.readErr != nil {
log.Errorf(ctx, "opening a reader %v on a closed channel", cid)
@@ -256,7 +258,9 @@ func (c *DataChannel) read(ctx context.Context) {
if local, ok := cache[id]; ok {
r = local
} else {
+ c.mu.Lock()
r = c.makeReader(ctx, id)
+ c.mu.Unlock()
cache[id] = r
}
@@ -306,10 +310,8 @@ func (r *errReader) Close() error {
return r.err
}
+// makeReader creates a dataReader. It expects to be called while c.mu is held.
func (c *DataChannel) makeReader(ctx context.Context, id clientID) *dataReader {
- c.mu.Lock()
- defer c.mu.Unlock()
-
if r, ok := c.readers[id]; ok {
return r
}