fix(gotenberg): leave a process alone when the memory watch ends while reading it

This commit is contained in:
Julien Neuhart
2026-11-17 19:22:52 +01:00
parent 48ad2a7227
commit 740079f98b
2 changed files with 16 additions and 7 deletions

View File

@@ -153,7 +153,9 @@ func WatchMemory(ctx context.Context, logger *slog.Logger, pid int, limit uint64
return
}
if used > limit {
// ctx may have ended while the tree was read: the work it watched is
// over, and so is the reason to call exceeded.
if used > limit && ctx.Err() == nil {
exceeded(used)
return
}

View File

@@ -37,20 +37,27 @@ func TestChromiumBrowser_WatchMemory(t *testing.T) {
}
t.Run("idle, not watched", func(t *testing.T) {
cmd, exited := start(t)
b := &chromiumBrowser{arguments: browserArguments{maxMemory: 1}, pid: cmd.Process.Pid}
// A conversion runs between the watch and its release, so the watch
// may read, and kill, the first process before the release: that is
// the in-flight case. The browser the supervisor runs next, idle,
// must not be watched.
first, _ := start(t)
b := &chromiumBrowser{arguments: browserArguments{maxMemory: 1}, pid: first.Process.Pid}
b.watchMemory(logger)()
if b.inFlight != 0 || b.stopWatching != nil {
t.Fatalf("inFlight = %d, stopWatching set = %t, want no watch once released", b.inFlight, b.stopWatching != nil)
}
next, exited := start(t)
b.pid = next.Process.Pid
select {
case <-exited:
t.Fatal("the watch killed the browser once no conversion was in flight")
case <-time.After(5 * gotenberg.MemoryWatchInterval):
}
if b.memoryExceeded.Load() != 0 {
t.Fatal("memoryExceeded set without a conversion in flight")
}
})
t.Run("in flight, over the limit", func(t *testing.T) {