Skip to content

Auto-rebuild listener on fatal socket errors and refactor listener creation#44

Merged
CypherPotato merged 1 commit into
mainfrom
codex/read-issue-43-and-its-derivation
May 29, 2026
Merged

Auto-rebuild listener on fatal socket errors and refactor listener creation#44
CypherPotato merged 1 commit into
mainfrom
codex/read-issue-43-and-its-derivation

Conversation

@CypherPotato
Copy link
Copy Markdown
Member

Motivation

  • Improve robustness of the TCP listener by detecting fatal socket errors during accept and automatically rebuilding the listening socket so the host can recover without full process restart.
  • Reduce duplication and make socket configuration explicit by centralizing listener creation and options.

Description

  • Replace readonly _listener with a mutable socket and introduce CreateListenerSocket() to centralize socket construction and options previously in ConfigureListenerSocket().
  • On Start() dispose the previous listener (if any) and recreate a fresh listener, then bind and listen before starting the accept pool.
  • Add _listenerRestarting flag and logic in StartAccept(), OnAcceptCompleted() and ProcessAcceptInline() to detect fatal SocketError values via IsListenerFatalError() and trigger RebuildListenerAsync() which retries creating/binding a new listener and restarts accept loops.
  • Return negative rearm values from ProcessAcceptInline() to indicate caller should stop rearming and handle restart scenarios, and adjust accept-queueing logic accordingly.

Testing

  • No automated tests were executed as part of this change.

Codex Task

Copy link
Copy Markdown

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

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: 0e5d53d365

ℹ️ 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 +273 to +274
catch (SocketException) {
}
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 Dispose failed rebuilt listener before retry loop

When CreateListenerSocket()/Bind/Listen throws during RebuildListenerAsync, the catch (SocketException) block swallows the exception and immediately retries without disposing the just-created newListener. In a persistent failure (e.g., endpoint temporarily unavailable), this leaks one socket handle every 250ms, which can exhaust file descriptors and prevent the host from recovering.

Useful? React with 👍 / 👎.

@CypherPotato
Copy link
Copy Markdown
Member Author

@codex work on this comment

@chatgpt-codex-connector
Copy link
Copy Markdown

Summary

  • Fixed the listener-rebuild retry path to avoid socket handle leaks when CreateListenerSocket(), Bind, or Listen fails with a SocketException by hoisting newListener outside the try and disposing it in the catch before the next retry.
  • Kept the existing successful-path behavior intact (_listener = newListener; break;) so accept-loop restart semantics are unchanged while adding cleanup only on failure.
  • Committed the fix on the current branch (5875b07) and created a follow-up PR message via the make_pr tool describing the change and rationale.

Testing

  • cd /workspace/core && rg -n "RebuildListenerAsync|CreateListenerSocket|SocketException|newListener" cadente/Sisk.Cadente/HttpHost.cs
  • cd /workspace/core && git diff -- cadente/Sisk.Cadente/HttpHost.cs
  • cd /workspace/core && git add cadente/Sisk.Cadente/HttpHost.cs && git commit -m "Dispose failed rebuilt listener before retry"
  • cd /workspace/core && nl -ba cadente/Sisk.Cadente/HttpHost.cs | sed -n '258,286p'
  • ✅ Web check performed: search_query("C# SocketException retry loop dispose socket on bind listen failure")

View task →

@CypherPotato CypherPotato merged commit 6b066d4 into main May 29, 2026
3 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant