Skip to content

fix: jsocket reconnect lifecycle 2.0.2#27

Merged
chris-piekarski merged 12 commits into
masterfrom
fix/jsocket-reconnect-lifecycle-2.0.2
Feb 15, 2026
Merged

fix: jsocket reconnect lifecycle 2.0.2#27
chris-piekarski merged 12 commits into
masterfrom
fix/jsocket-reconnect-lifecycle-2.0.2

Conversation

@chris-piekarski
Copy link
Copy Markdown
Owner

This PR hardens jsocket connection lifecycle handling and removes an avoidable reconnect penalty after JsonClient.close(). It also
improves server-side socket/wakeup cleanup and bumps the package version to 2.0.2.

What Changed

  • jsocket/jsocket_base.py
  • JsonClient.connect() now pre-checks socket state and recreates a fresh socket when the current fd is closed (fileno() == -1)
    before attempting connect().
  • Avoids the forced first-attempt Bad file descriptor error + 3s backoff cycle when reusing the same client object after close().
  • Keeps retry/backoff behavior for genuine connection failures.
  • _send() now treats OSError/zero-byte sends as broken connection and closes cleanly.
  • Added _is_listening tracking so server listen() is idempotent and listener state is reset on bind/close.
  • jsocket/tserver.py
  • Wakeup socket fallback now closes intermediate sockets reliably on failure/success paths.
  • ServerFactory now creates worker threads only after a successful accept/handoff.
  • Accept-loop logging keeps non-timeout unexpected failures at exception level; shutdown path remains quiet (debug).
  • tests/test_additional_coverage.py
  • Added regression test to verify reconnecting with a previously closed client socket does not hit backoff sleep.
  • jsocket/_version.py
  • Version bumped from 2.0.1 to 2.0.2.

Behavioral Impact

  • Reusing a JsonClient instance after close() is now fast-path recoverable (fresh socket created before dialing), instead of
    guaranteed initial failure + delay.
  • Server/listener lifecycle is more robust under churn and shutdown.

Validation

  • pytest -q tests/test_additional_coverage.py::test_client_connect_failure_returns_false tests/
    test_additional_coverage.py::test_client_connect_recreates_closed_socket_without_backoff (1 passed, 1 skipped in sandboxed env).
  • pytest -q tests/test_additional_coverage.py::test_serverfactory_accept_error_branch tests/
    test_additional_coverage.py::test_serverfactory_accept_timeout_and_close_branches (1 passed, 1 skipped in sandboxed env).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens jsocket client/server socket lifecycle behavior to avoid unnecessary reconnect backoff after JsonClient.close(), and improves server-side cleanup and accept-loop behavior while bumping the package version to 2.0.2.

Changes:

  • JsonClient.connect() now recreates sockets proactively when the existing fd is closed, avoiding a guaranteed initial failure/backoff after reuse.
  • _send() treats OSError/zero-byte sends as a broken connection and closes cleanly; server listening is made idempotent via _is_listening.
  • tserver wakeup-socket fallback cleanup and ServerFactory accept/worker-spawn sequencing are improved; adds a regression test and bumps version.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
jsocket/jsocket_base.py Adds closed-fd detection + socket recreation in connect(), hardens _send(), and tracks/reset server listening state.
jsocket/tserver.py Improves wakeup socket fallback cleanup, adjusts accept-loop logging, and spawns workers only after successful accept/handoff.
tests/test_additional_coverage.py Adds regression coverage for reconnect-after-close avoiding the backoff sleep path.
jsocket/_version.py Bumps package version to 2.0.2.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread jsocket/tserver.py Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@chris-piekarski chris-piekarski merged commit 8c3ea23 into master Feb 15, 2026
5 checks passed
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