fix(gateway): retain Devin reply channel while awaiting (#48)

The response reader can delete a pending call before its caller looks up the channel, causing intermittent no such call errors. Return the reply channel from send so await can receive even after the reader removes the pending entry.


(cherry picked from commit 6278c793b9b4f45dc09106168269915007abffe5)

Signed-off-by: jimyag <git@jimyag.com>
This commit is contained in:
jimyag
2026-09-25 08:30:46 +08:00
committed by GitHub
parent 00b173682f
commit c8533f40ff
2 changed files with 13 additions and 19 deletions
+9 -15
View File
@@ -167,12 +167,12 @@ func (b *subscriptionBridge) startDevin(ctx context.Context, req *Request, model
if err != nil {
return fail(err)
}
id, err := conn.send("session/prompt", map[string]any{"sessionId": sess.SessionID, "prompt": prompt})
_, reply, err := conn.send("session/prompt", map[string]any{"sessionId": sess.SessionID, "prompt": prompt})
if err != nil {
return fail(err)
}
go func() {
res, err := conn.await(context.Background(), id)
res, err := conn.await(context.Background(), reply)
if err != nil {
run.emit(Event{Kind: KError, Text: "devin: " + err.Error()})
} else {
@@ -229,11 +229,11 @@ type devinRPCError struct {
func (e *devinRPCError) Error() string { return e.Message }
func (c *devinConn) send(method string, params any) (int64, error) {
func (c *devinConn) send(method string, params any) (int64, <-chan devinReply, error) {
id := c.seq.Add(1)
b, err := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": id, "method": method, "params": params})
if err != nil {
return 0, err
return 0, nil, err
}
ch := make(chan devinReply, 1)
c.mu.Lock()
@@ -246,18 +246,12 @@ func (c *devinConn) send(method string, params any) (int64, error) {
c.mu.Lock()
delete(c.pending, id)
c.mu.Unlock()
return 0, err
return 0, nil, err
}
return id, nil
return id, ch, nil
}
func (c *devinConn) await(ctx context.Context, id int64) (json.RawMessage, error) {
c.mu.Lock()
ch := c.pending[id]
c.mu.Unlock()
if ch == nil {
return nil, errors.New("no such call")
}
func (c *devinConn) await(ctx context.Context, ch <-chan devinReply) (json.RawMessage, error) {
select {
case r := <-ch:
return r.result, r.err
@@ -267,11 +261,11 @@ func (c *devinConn) await(ctx context.Context, id int64) (json.RawMessage, error
}
func (c *devinConn) call(ctx context.Context, method string, params any) (json.RawMessage, error) {
id, err := c.send(method, params)
_, ch, err := c.send(method, params)
if err != nil {
return nil, err
}
return c.await(ctx, id)
return c.await(ctx, ch)
}
func (c *devinConn) answer(id json.RawMessage, result any) {
+4 -4
View File
@@ -82,7 +82,7 @@ func (p devinPair) next(t *testing.T) map[string]any {
func TestDevinConnCallsAndAnswers(t *testing.T) {
p := newDevinPair(t)
id, err := p.conn.send("initialize", map[string]any{"protocolVersion": 1})
id, reply, err := p.conn.send("initialize", map[string]any{"protocolVersion": 1})
if err != nil {
t.Fatal(err)
}
@@ -92,7 +92,7 @@ func TestDevinConnCallsAndAnswers(t *testing.T) {
}
// a response resolves the call
p.write(t, fmt.Sprintf(`{"jsonrpc":"2.0","id":%d,"result":{"protocolVersion":1}}`, id))
res, err := p.conn.await(context.Background(), id)
res, err := p.conn.await(context.Background(), reply)
if err != nil || !strings.Contains(string(res), "protocolVersion") {
t.Fatalf("await: %v %s", err, res)
}
@@ -128,9 +128,9 @@ func TestDevinConnCallsAndAnswers(t *testing.T) {
t.Fatalf("events: %v", kinds)
}
// the stream ending fails a call still open
id2, _ := p.conn.send("session/prompt", map[string]any{})
_, reply2, _ := p.conn.send("session/prompt", map[string]any{})
p.toConn.Close()
if _, err := p.conn.await(context.Background(), id2); err == nil {
if _, err := p.conn.await(context.Background(), reply2); err == nil {
t.Fatal("an open call survived the end of the stream")
}
}