Skip to content

Disconnects (not just real reconnects) rebuild the whole IoConnector and its thread pool in SocketInitiator #1354

Description

@petrikh-artur

Describe the bug

We've been chasing a slow memory/thread churn problem in a bunch of FIX initiators that spend long stretches unable to log in to a counterparty (wrong creds during a cutover, counterparty down, logon rejected, etc.), and tracked it down to IoSessionInitiator.

Any time a Session disconnects — failed logon, logout, heartbeat timeout, whatever — IoSessionResponder.disconnect() unconditionally does:

public void disconnect() {
    ioSession.closeOnFlush();
    ioSession.setAttribute(SessionConnector.QFJ_RESET_IO_CONNECTOR, Boolean.TRUE);
}

and on the next tick of the reconnect timer, ConnectTask.resetIoConnector() sees that flag and calls setupIoConnector(), which builds a brand new IoConnector:

IoConnector newConnector = ProtocolFactory.createIoConnector(socketAddresses[nextSocketAddressIndex]);
...
if (ioConnector != null) {
    SessionConnector.closeManagedSessionsAndDispose(ioConnector, true, log);
}
ioConnector = newConnector;

ProtocolFactory.createIoConnector for a socket address is just new NioSocketConnector(), which spins up its own SimpleIoProcessorPool (defaults to cores + 1 threads) and selector. So every single disconnect/reconnect cycle tears down and rebuilds the whole networking stack for that session instead of just reusing the existing connector and calling connect() again.

This only kicks in once a session has actually connected at least once. A pure "can't reach the host at all" scenario (connection refused/unreachable) never creates an IoSession in the first place, so the flag never gets set and this path never fires — we only noticed it once we tested against a counterparty that accepts the TCP connection but then rejects/drops the session during logon, which is a pretty normal failure mode in practice.

To Reproduce

Minimal repro: point a SocketInitiator at a plain ServerSocket that accepts the connection, reads whatever bytes come in, and closes the socket without ever replying (so Logon never gets a response and the session just drops). With ReconnectInterval=1, after a few hundred cycles, jcmd <pid> Thread.print shows thread names like NioSocketConnector-834 and pool-1667-thread-1 — the counters tell you a few hundred NioSocketConnector instances (and roughly 2x that many executor pools) got created and torn down over the run. Live heap after a forced GC.run also climbs over the first several minutes (we saw ~3.5MB -> ~9MB over the first ~800 cycles) before the growth rate tapers off, on top of the obvious thread/selector/GC churn.

Expected behavior

On a normal disconnect (no address/proxy/SSL config change involved), the initiator should just reconnect using the existing IoConnector/processor pool instead of disposing it and building a new one from scratch. Rebuilding the whole connector seems like it should be reserved for cases where something about the connector's configuration actually changed.

Additional context

This matters more than it looks at first glance because the trigger isn't "can't connect at all" — it's any session teardown, which is routine with flaky counterparties, scheduled maintenance, or slow/rejected logons. A session that's struggling to log in for a few hours ends up rebuilding its entire connector (and thread pool) every ReconnectInterval seconds for that whole time.

Not sure if resetIoConnector() tearing down the whole IoConnector on every disconnect is intentional (maybe needed for some dynamic-session/proxy-reconfiguration case we're not hitting?), but for a plain static session the existing connector already handles repeated failed connect() calls fine — that's exactly what happens when the host is unreachable from the start, no IoSession ever gets created and the same ioConnector just keeps retrying without issue. So a disconnect after a successful connect doesn't seem to need a full rebuild either, unless something about the connector's own config (proxy, SSL, local address) actually changed. Happy to be told why it's done this way if there's a reason we're missing.

Activity

  1. chrjohn commented on Oct 21, 2026

    @chrjohn
    Member

    Hi @petrikh-artur , thanks for the detailed report. I'll need to check why it is implemented this way.

  2. changed the title [-]Disconnects (not just real reconnects) rebuild the whole IoConnector and its thread pool in SocketInitiator[/-] [+]Disconnects (not just real reconnects) rebuild the whole `IoConnector` and its thread pool in `SocketInitiator`[/+] on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions