Skip to content

Commit a80af34

Browse files
authored
Merge branch 'main' into fix/write-stabilize-non-fatal-phase1
2 parents f6f14b8 + d42b9ac commit a80af34

File tree

4 files changed

+17
-25
lines changed

4 files changed

+17
-25
lines changed

cmd/attach/attach.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
tea "github.com/charmbracelet/bubbletea"
1616
"github.com/coder/agentapi/lib/httpapi"
17-
"github.com/coder/agentapi/lib/util"
1817
"github.com/coder/quartz"
1918
"github.com/spf13/cobra"
2019
sse "github.com/tmaxmax/go-sse"
@@ -239,9 +238,11 @@ func runAttach(remoteURL string) error {
239238
}
240239

241240
p.Send(finishMsg{})
241+
graceTimer := quartz.NewReal().NewTimer(1 * time.Second)
242+
defer graceTimer.Stop()
242243
select {
243244
case <-pErrCh:
244-
case <-util.After(quartz.NewReal(), 1*time.Second):
245+
case <-graceTimer.C:
245246
}
246247

247248
return err

lib/screentracker/pty_conversation.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,14 @@ func (c *PTYConversation) writeStabilize(ctx context.Context, messageParts ...Me
461461
}, func() (bool, error) {
462462
screen := c.cfg.AgentIO.ReadScreen()
463463
if screen != screenBeforeMessage {
464+
stabilityTimer := c.cfg.Clock.NewTimer(1 * time.Second)
464465
select {
465466
case <-ctx.Done():
467+
stabilityTimer.Stop()
466468
return false, ctx.Err()
467-
case <-util.After(c.cfg.Clock, 1*time.Second):
469+
case <-stabilityTimer.C:
468470
}
471+
stabilityTimer.Stop()
469472
newScreen := c.cfg.AgentIO.ReadScreen()
470473
return newScreen == screen, nil
471474
}
@@ -502,11 +505,14 @@ func (c *PTYConversation) writeStabilize(ctx context.Context, messageParts ...Me
502505
return false, xerrors.Errorf("failed to write carriage return: %w", err)
503506
}
504507
}
508+
crTimer := c.cfg.Clock.NewTimer(25 * time.Millisecond)
505509
select {
506510
case <-ctx.Done():
511+
crTimer.Stop()
507512
return false, ctx.Err()
508-
case <-util.After(c.cfg.Clock, 25*time.Millisecond):
513+
case <-crTimer.C:
509514
}
515+
crTimer.Stop()
510516
screen := c.cfg.AgentIO.ReadScreen()
511517

512518
return screen != screenBeforeCarriageReturn, nil

lib/termexec/termexec.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ func (p *Process) ReadScreen() string {
127127
return state
128128
}
129129
p.screenUpdateLock.RUnlock()
130-
<-util.After(p.clock, 16*time.Millisecond)
130+
t := p.clock.NewTimer(16 * time.Millisecond)
131+
<-t.C
132+
t.Stop()
131133
}
132134
return p.xp.State.String()
133135
}
@@ -152,9 +154,11 @@ func (p *Process) Close(logger *slog.Logger, timeout time.Duration) error {
152154
close(exited)
153155
}()
154156

157+
timeoutTimer := p.clock.NewTimer(timeout)
158+
defer timeoutTimer.Stop()
155159
var exitErr error
156160
select {
157-
case <-util.After(p.clock, timeout):
161+
case <-timeoutTimer.C:
158162
if err := p.execCmd.Process.Kill(); err != nil {
159163
exitErr = xerrors.Errorf("failed to forcefully kill the process: %w", err)
160164
}

lib/util/util.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,3 @@ func OpenAPISchema[T ~string](r huma.Registry, enumName string, values []T) *hum
9999
}
100100
return &huma.Schema{Ref: fmt.Sprintf("#/components/schemas/%s", enumName)}
101101
}
102-
103-
// After is a convenience function that returns a channel that will send the
104-
// time after the given duration has elapsed using the provided clock.
105-
// If clk is nil, a real clock will be used by default.
106-
// Note that this function spawns a goroutine that will remain alive until the
107-
// timer fires.
108-
func After(clk quartz.Clock, d time.Duration) <-chan time.Time {
109-
if clk == nil {
110-
clk = quartz.NewReal()
111-
}
112-
timer := clk.NewTimer(d)
113-
ch := make(chan time.Time)
114-
go func() {
115-
defer timer.Stop()
116-
defer close(ch)
117-
ch <- <-timer.C
118-
}()
119-
return ch
120-
}

0 commit comments

Comments
 (0)