Skip to content

Commit 88a6b96

Browse files
committed
Remove err from wait callback
We do not really do anything with it other than just return it, and if we do need to do something with it we can just do it on the return from `waitForStateOrContext`, we probably would not need to use it behind the mutex. Also we should check the state outside since there is technically no guarantee an error is set on a state change (although in practice for close/done there always is, maybe this should be enforced in some way).
1 parent 34c5c1a commit 88a6b96

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

agent/reconnectingpty/buffered.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,19 @@ func (rpty *bufferedReconnectingPTY) Attach(ctx context.Context, connID string,
179179
defer cancel()
180180

181181
// Once we are ready, attach the active connection while we hold the mutex.
182-
_, err := rpty.state.waitForStateOrContext(ctx, StateReady, func(state State, err error) error {
182+
state, err := rpty.state.waitForStateOrContext(ctx, StateReady, func(state State) error {
183183
// Write any previously stored data for the TTY. Since the command might be
184184
// short-lived and have already exited, make sure we always at least output
185185
// the buffer before returning.
186186
prevBuf := slices.Clone(rpty.circularBuffer.Bytes())
187-
_, writeErr := conn.Write(prevBuf)
188-
if writeErr != nil {
187+
_, err := conn.Write(prevBuf)
188+
if err != nil {
189189
rpty.metrics.WithLabelValues("write").Add(1)
190-
return xerrors.Errorf("write buffer to conn: %w", writeErr)
190+
return xerrors.Errorf("write buffer to conn: %w", err)
191191
}
192192

193193
if state != StateReady {
194-
return err
194+
return nil
195195
}
196196

197197
go heartbeat(ctx, rpty.timer, rpty.timeout)
@@ -209,7 +209,7 @@ func (rpty *bufferedReconnectingPTY) Attach(ctx context.Context, connID string,
209209

210210
return nil
211211
})
212-
if err != nil {
212+
if state != StateReady || err != nil {
213213
return xerrors.Errorf("reconnecting pty ready wait: %w", err)
214214
}
215215

agent/reconnectingpty/reconnectingpty.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ func (s *ptyState) waitForState(state State) (State, error) {
168168

169169
// waitForStateOrContext blocks until the state or a greater one is reached or
170170
// the provided context ends. If fn is non-nil it will be ran while the lock is
171-
// held unless the context ends, and fn's error will replace
172-
// waitForStateOrContext's error.
173-
func (s *ptyState) waitForStateOrContext(ctx context.Context, state State, fn func(state State, err error) error) (State, error) {
171+
// held unless the context ends. If fn returns an error then fn's error will
172+
// replace waitForStateOrContext's error.
173+
func (s *ptyState) waitForStateOrContext(ctx context.Context, state State, fn func(state State) error) (State, error) {
174174
nevermind := make(chan struct{})
175175
defer close(nevermind)
176176
go func() {
@@ -191,7 +191,10 @@ func (s *ptyState) waitForStateOrContext(ctx context.Context, state State, fn fu
191191
return s.state, ctx.Err()
192192
}
193193
if fn != nil {
194-
return s.state, fn(s.state, s.error)
194+
err := fn(s.state)
195+
if err != nil {
196+
return s.state, err
197+
}
195198
}
196199
return s.state, s.error
197200
}

0 commit comments

Comments
 (0)