Skip to content

email: fix PR #103 review — RetrievePeek + bounded shutdown timeouts - #105

Merged
FMXExpress merged 1 commit into
mainfrom
claude/pr103-review-fixes
May 31, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/pr103-review-fixes

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

Two Codex P2 findings on 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 silently dropped.

Fix: switched to IMAP.RetrievePeek (FETCH BODY.PEEK[]), which doesn't set Seen as a side effect. The explicit StoreFlags now becomes the only place mfSeen is added, so the PR #103 truth table actually works end-to-end:

ReplyAttempted ReplySent StoreFlags? Result
False (no reply) — Yes Don't loop on poison
True True Yes Delivered
True False No Now actually retried next poll

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. 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.

  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 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.

Verification

make clean && make smoke 11/11 green.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

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
@FMXExpress
FMXExpress merged commit 95d78fc into main May 31, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +535 to +536
FWorker.FreeOnTerminate := True;
FWorker := nil;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants