Skip to content

Commit 0719927

Browse files
buddektoso
authored andcommitted
Avoid arithmetic overflow for mailboxCount metric
1 parent 06e4015 commit 0719927

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Sources/DistributedActors/Mailbox.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,19 @@ internal final class Mailbox<Message: ActorMessage> {
486486
// MUST be the first check, as we may want to stop immediately (e.g. reacting to system .start a with .stop),
487487
// as other conditions may hold, yet we really are ready to terminate immediately.
488488
traceLog_Mailbox(shell.path, "Terminating...")
489-
shell.metrics[gauge: .mailboxCount]?.record(status.messageCount - Status(processedActivations).messageCount)
489+
let processedActivationsCount = Status(processedActivations).messageCount
490+
if status.messageCount >= processedActivationsCount {
491+
shell.metrics[gauge: .mailboxCount]?.record(status.messageCount - processedActivationsCount)
492+
} else {
493+
// TODO: Figure out why this can ever happen
494+
shell.log.warning(
495+
"Mailbox closed with more processed activations (\(processedActivationsCount)) than messages (\(status.messageCount))",
496+
metadata: [
497+
"status": "\(status)",
498+
"processedActivations": "\(processedActivations)",
499+
]
500+
)
501+
}
490502
return .close
491503
} else if runResult == .closed {
492504
traceLog_Mailbox(shell.path, "Terminating, completely closed now...")

0 commit comments

Comments
 (0)