Repository navigation
email: fix PR #103 review — RetrievePeek + bounded shutdown timeouts - #105
Conversation
Two Codex P2 findings on the merged PR #103, both legitimate. P2 — IMAP.Retrieve auto-marks Seen, defeating the failed-SMTP retry logic. PR #103 introduced a ReplySent / ReplyAttempted truth table that intentionally skipped StoreFlags(mfSeen) when SMTP delivery failed, on the theory that the next SEARCH UNSEEN would re-list the message. But Retrieve sends FETCH BODY[], which the IMAP RFC defines as a Seen-setting fetch — so the message left the unseen set the moment we read it, regardless of whether the StoreFlags later ran. Failed replies were dropped silently. Fix: switched to IMAP.RetrievePeek (FETCH BODY.PEEK[]) which doesn't set Seen as a side effect. Our explicit StoreFlags now becomes the only place mfSeen is added, so the truth table from PR #103 actually works end-to-end. P2 — TThread.WaitFor blocks indefinitely on shutdown. If the worker was inside a wedged IMAP.Retrieve or SMTP.Send when the gateway called Email.Stop, FStop got set but the worker never returned from the syscall, so WaitFor blocked the gateway indefinitely. The user-facing symptom was Ctrl-C requiring a second SIGKILL. Fix in two halves: 1. Bound the I/O at the source. TIdIMAP4 and TIdSMTP both inherit ConnectTimeout / ReadTimeout from TIdTCPClient; set both to EmailIOTimeoutMS (30s). After Stop signals FStop, the worst-case worker exit is one ReadTimeout plus the post-op return path — bounded, not unbounded. 2. Bound the WaitFor too. New WaitForWorkerWithTimeout helper polls Worker.Finished at 50ms granularity for up to EmailStopWatchdogMS (= EmailIOTimeoutMS + 5s). If the worker hasn't exited in that window — e.g. server stopped responding mid-TLS handshake before the ReadTimeout could trigger — Email.Stop sets FreeOnTerminate := True and detaches the thread. The gateway shuts down on schedule; the detached worker cleans itself up if/when it eventually unblocks. Better than wedging the gateway forever. Verified `make clean && make smoke` 11/11 green. https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5a1d4e2f8a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| FWorker.FreeOnTerminate := True; | ||
| FWorker := nil; |
There was a problem hiding this comment.
Keep the email channel alive until the worker exits
When shutdown starts while the worker is still inside a slow operation, this detaches the thread after 35 seconds even though TEmailWorker.Execute continues using its raw FChan reference. This is not limited to a wedged IMAP socket: the inspected RunToolLoop path can spend up to 120 seconds in a provider request, and src/cmd/PasClaw.Cmd.Gateway.pas:298 immediately calls Email.Free after Stop returns. Once the request resumes, the worker can dereference the destroyed channel and its released fields. Retain ownership until the worker exits or actively cancel the operation instead of abandoning a channel-bound thread.
Useful? React with 👍 / 👎.
| still returns this message and we retry. Retrieve would | ||
| silently mark Seen on fetch, draining the message from | ||
| the unseen set even though the reply never went out. } | ||
| if not IMAP.RetrievePeek(SeqNum, Msg) then Continue; |
There was a problem hiding this comment.
Avoid resending replies when marking Seen fails
When SMTP delivery succeeds but the subsequent IMAP StoreFlags request fails or the IMAP connection drops before it is issued, fetching with RetrievePeek leaves the message unread. The next SEARCH UNSEEN poll processes the same inbound message again and sends a duplicate reply, potentially repeating indefinitely during an IMAP-side outage. Preserve retry state for the Seen update, or otherwise distinguish an already-delivered reply from an SMTP failure before retrying the entire message.
Useful? React with 👍 / 👎.
Summary
Two Codex P2 findings on merged PR #103, both legitimate.
P2 —
IMAP.Retrieveauto-marks Seen, defeating the failed-SMTP retry logicPR #103 introduced a
ReplySent/ReplyAttemptedtruth table that intentionally skippedStoreFlags(mfSeen)when SMTP delivery failed, on the theory that the nextSEARCH UNSEENwould re-list the message. ButRetrievesendsFETCH BODY[], which the IMAP RFC defines as a Seen-setting fetch — so the message left the unseen set the moment we read it, regardless of whether theStoreFlagslater ran. Failed replies were silently dropped.Fix: switched to
IMAP.RetrievePeek(FETCH BODY.PEEK[]), which doesn't set Seen as a side effect. The explicitStoreFlagsnow becomes the only placemfSeenis added, so the PR #103 truth table actually works end-to-end:P2 —
TThread.WaitForblocks indefinitely on shutdownIf the worker was inside a wedged
IMAP.RetrieveorSMTP.Sendwhen the gateway calledEmail.Stop,FStopgot set but the worker never returned from the syscall — soWaitForblocked the gateway indefinitely. User-facing symptom was Ctrl-C requiring a second SIGKILL.Fix in two halves:
Bound the I/O at the source.
TIdIMAP4andTIdSMTPboth inheritConnectTimeout/ReadTimeoutfromTIdTCPClient; set both toEmailIOTimeoutMS(30s). AfterStopsignalsFStop, the worst-case worker exit is oneReadTimeoutplus the post-op return path — bounded.Bound the
WaitFortoo. NewWaitForWorkerWithTimeouthelper pollsWorker.Finishedat 50ms granularity for up toEmailStopWatchdogMS(=EmailIOTimeoutMS + 5s). If the worker hasn't exited in that window — e.g. server stopped responding mid-TLS handshake beforeReadTimeoutcould trigger —Email.StopsetsFreeOnTerminate := Trueand detaches the thread. The gateway shuts down on schedule; the detached worker cleans itself up if/when it eventually unblocks.Verification
make clean && make smoke11/11 green.https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
Generated by Claude Code