@@ -180,6 +180,16 @@ func (rpty *bufferedReconnectingPTY) Attach(ctx context.Context, connID string,
180
180
181
181
// Once we are ready, attach the active connection while we hold the mutex.
182
182
_ , err := rpty .state .waitForStateOrContext (ctx , StateReady , func (state State , err error ) error {
183
+ // Write any previously stored data for the TTY. Since the command might be
184
+ // short-lived and have already exited, make sure we always at least output
185
+ // the buffer before returning.
186
+ prevBuf := slices .Clone (rpty .circularBuffer .Bytes ())
187
+ _ , writeErr := conn .Write (prevBuf )
188
+ if err != nil {
189
+ rpty .metrics .WithLabelValues ("write" ).Add (1 )
190
+ return xerrors .Errorf ("write buffer to conn: %w" , writeErr )
191
+ }
192
+
183
193
if state != StateReady {
184
194
return xerrors .Errorf ("reconnecting pty ready wait: %w" , err )
185
195
}
@@ -194,14 +204,7 @@ func (rpty *bufferedReconnectingPTY) Attach(ctx context.Context, connID string,
194
204
rpty .metrics .WithLabelValues ("resize" ).Add (1 )
195
205
}
196
206
197
- // Write any previously stored data for the TTY and store the connection for
198
- // future writes.
199
- prevBuf := slices .Clone (rpty .circularBuffer .Bytes ())
200
- _ , err = conn .Write (prevBuf )
201
- if err != nil {
202
- rpty .metrics .WithLabelValues ("write" ).Add (1 )
203
- return xerrors .Errorf ("write buffer to conn: %w" , err )
204
- }
207
+ // Store the connection for future writes.
205
208
rpty .activeConns [connID ] = conn
206
209
207
210
return nil
0 commit comments