Skip to content

Sockets: fixes to ClientSocket_Qt - #1784

Merged
Mysticial merged 2 commits into
mainfrom
sockets-qt-disconnect
Oct 31, 2026
Merged

Mysticial merged 2 commits into
mainfrom
sockets-qt-disconnect

Conversation

@pa-devbot

Copy link
Copy Markdown
Collaborator

Part of the socket state-machine fixes; the on_disconnect() hook landed in #1780. This is the implementation currently in use on Windows.

What was wrong

  • Connect failures were silent. errorOccurred/disconnected only set DESTRUCTING; on_connect_finished(error) was never called, so a refused connection (e.g. a wrong IP) left the status line stuck on "Connecting..." forever and the socket unusable.
  • Connection loss was invisible. An established connection that ended on its own never fired a callback and left the state stuck.
  • Reconnect after close() was a silent no-op. close() destroyed the QTcpSocket (and with it all of its slots), so connect()/send() afterwards did nothing at all.
  • send() lied about the result. It returned the requested size without waiting for the write or checking anything.
  • readyRead only read one 4096-byte chunk per event, so a burst bigger than one buffer could sit in the socket until the next event.

What this does

  • Connect failures (errorOccurred/disconnected while connecting) now fire on_connect_finished(error_message) and return the socket to NOT_RUNNING.
  • An established connection that ends on its own fires on_disconnect(error_message) and returns to NOT_RUNNING, so connect() can be called again.
  • Connect success is confirmed with a 50 ms grace period: on some platforms Qt emits connected() optimistically, and a refused connection emits connected() followed immediately by errorOccurred() (verified with Qt 6.10 on Linux), so the success callback only fires if no error arrives in the meantime.
  • connect() always creates a fresh QTcpSocket on the Qt event-thread pool and destroys the previous one; close() is blocking and idempotent.
  • send() blocks until the write completes and returns the number of bytes actually handed to the kernel (0 if the connection is gone).
  • readyRead drains all available data in a bytesAvailable() loop.

Testing

Full functional test against a real TCP server (loopback, Qt 6.10.2): connect, send, receive (including a 20 KB burst through the drain loop), graceful close, RST, refused connect (reports "Connection refused" through on_connect_finished), close() + reconnect, and 50 rapid connect/disconnect cycles - all pass, stable across repeated runs.

pa-devbot and others added 2 commits October 31, 2026 09:26
Ports the same state-machine fixes as the WinSocket/POSIX PRs to the Qt
implementation:

- Report connect failures: errorOccurred/disconnected during a connect
  attempt now fire on_connect_finished(error_message) and return the
  socket to NOT_RUNNING, instead of leaving the state stuck at
  CONNECTING/DESTRUCTING (a refused connection used to leave the UI on
  'Connecting...' forever).
- Detect connection loss: an established connection that ends on its
  own (server close, reset) now fires on_disconnect(error_message) and
  returns to NOT_RUNNING instead of staying unusable.
- Confirm connect success with a short grace period: on some platforms
  Qt emits connected() optimistically, and a refused connection emits
  connected() followed immediately by errorOccurred(). The success
  callback is now only fired 50 ms after connected(), if no error
  arrived in the meantime.
- Reconnect after close(): close() used to destroy the QTcpSocket (and
  all of its slots), after which connect()/send() were silent no-ops.
  connect() now always creates a fresh QTcpSocket on the event thread
  pool, and destroys the previous one. close() is idempotent.
- send() now actually reports how many bytes were written and blocks
  until the write completes (previously it returned the requested size
  without waiting for anything). A failed or closed connection returns
  the number of bytes that made it to the kernel.
- readyRead now drains all available data (a single delivery can hold
  more than one 4096-byte buffer).
@Mysticial
Mysticial merged commit 9a29aa3 into main Oct 31, 2026
7 checks passed
@Mysticial
Mysticial deleted the sockets-qt-disconnect branch October 31, 2026 10:41
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