mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-12-06 16:22:15 +00:00
fix(gotenberg): leave a process alone when the memory watch ends while reading it
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user