mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
devvm desktop's relay returned at any error from its listener's Accept, so running out of open files in a burst (EMFILE) ended devvm desktop. It now waits and tries again, as devvm's other accept loops do, with their one owner, daemon.AcceptRetry: 5 ms, doubling up to a second, logging only the first error of a run, and it still ends with its context or its listener's close. - internal/desktop/relay.go: Serve uses daemon.AcceptRetry. - Tests: TestRelayAcceptRetries (a listener that fails three Accepts with EMFILE, then carries a viewer; the run logged once). - Docs: processes' desktop relay, structure's internal/daemon row; CHANGELOG Fixed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
51 lines
1.7 KiB
Go
51 lines
1.7 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
// The waits of an accept loop between tries after errors that leave its listener open
|
|
// (AcceptRetry): the first, doubled after each error in a row up to the last.
|
|
const (
|
|
acceptRetryFirst = 5 * time.Millisecond
|
|
acceptRetryMax = time.Second
|
|
)
|
|
|
|
// AcceptRetry is an accept loop's state between errors from its listener's Accept, its zero value
|
|
// before any. Each loop has its own. The background processes' loops share it, the port
|
|
// forwarder's, the link session's and the proxy's SSH agent relay, and so do a login's forward and
|
|
// devvm desktop's relay.
|
|
type AcceptRetry struct{ wait time.Duration }
|
|
|
|
// Failed is what an accept loop does with err, from its listener's Accept: false once the listener
|
|
// is closed or ctx has ended, so the loop ends. Any other error, such as running out of files
|
|
// (EMFILE) in a burst, leaves the listener open and listening, so that ending the loop would leave
|
|
// its port unanswered: the loop tries again (true) after a wait that starts at acceptRetryFirst and
|
|
// doubles with each error in a row, up to acceptRetryMax, as what's accepted goes on. Only the
|
|
// first error of a run goes to log, when there is one, so a run of them can't fill it.
|
|
func (a *AcceptRetry) Failed(ctx context.Context, err error, log func(error)) bool {
|
|
if errors.Is(err, net.ErrClosed) || ctx.Err() != nil {
|
|
return false
|
|
}
|
|
if a.wait == 0 {
|
|
if log != nil {
|
|
log(err)
|
|
}
|
|
a.wait = acceptRetryFirst
|
|
} else {
|
|
a.wait = min(2*a.wait, acceptRetryMax)
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return false
|
|
case <-time.After(a.wait):
|
|
return true
|
|
}
|
|
}
|
|
|
|
// Accepted ends a run of errors: the loop accepted a connection.
|
|
func (a *AcceptRetry) Accepted() { a.wait = 0 }
|