Skip to content

Commit 8a28fe6

Browse files
gateway: wrap ExecProcessServer Send calls with a mutex
According to gRPC ClientStream interface documentation it isn't safe to call SendMsg on the same stream in different goroutines, which we do in LLBBridgeServer implementation. The generated bindings don't provide a mechanism to avoid this scenario. This change wraps LLBBridgeServer implementation with a custom locking behavior: https://pkg.go.dev/google.golang.org/[email protected]#ClientStream.SendMsg Signed-off-by: Matias Insaurralde <[email protected]>
1 parent 3c4e8f5 commit 8a28fe6

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

frontend/gateway/gateway.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,21 @@ func (w *outputWriter) Write(msg []byte) (int, error) {
11761176
return len(msg), stack.Enable(err)
11771177
}
11781178

1179+
type execProcessServerThreadSafe struct {
1180+
pb.LLBBridge_ExecProcessServer
1181+
sendMu sync.Mutex
1182+
}
1183+
1184+
func (w *execProcessServerThreadSafe) Send(m *pb.ExecMessage) error {
1185+
w.sendMu.Lock()
1186+
defer w.sendMu.Unlock()
1187+
return w.LLBBridge_ExecProcessServer.Send(m)
1188+
}
1189+
11791190
func (lbf *llbBridgeForwarder) ExecProcess(srv pb.LLBBridge_ExecProcessServer) error {
1191+
srv = &execProcessServerThreadSafe{
1192+
LLBBridge_ExecProcessServer: srv,
1193+
}
11801194
eg, ctx := errgroup.WithContext(srv.Context())
11811195

11821196
msgs := make(chan *pb.ExecMessage)

0 commit comments

Comments
 (0)