Bug/Quality: Protocol layer uses global I/O state; io_ssl is not thread-local #165

Closed
opened 2026-07-29 18:36:29 +02:00 by TapTap · 0 comments
Owner

Description

The protocol layer in src/shared/protocol.c uses module-global state for the active connection: io_read_fd, io_write_fd are static __thread (thread-local), but io_ssl is a plain static global. This means all threads in the same process share the same SSL* object, which is not thread-safe. Although the server currently forks per TCP connection, the multithreaded server path (server.c:84-111) and the multithreaded client pipeline could race on io_ssl.

Furthermore, the global state makes the protocol functions non-reentrant and harder to unit test.

Location

  • src/shared/protocol.c:13static SSL* io_ssl = NULL; should be static __thread SSL* io_ssl = NULL;.
  • src/shared/protocol.c:11-17 — all I/O state is global.

Suggested fix

  1. Make io_ssl thread-local, or better:
  2. Refactor send_n_data / receive_n_data to take an explicit I/O context struct (containing read fd, write fd, SSL pointer, and bandwidth limit) instead of using global state.

Severity

Medium

Category

bug, quality


This issue was automatically generated by the issue-creator agent.

## Description The protocol layer in `src/shared/protocol.c` uses module-global state for the active connection: `io_read_fd`, `io_write_fd` are `static __thread` (thread-local), but `io_ssl` is a plain `static` global. This means all threads in the same process share the same `SSL*` object, which is not thread-safe. Although the server currently forks per TCP connection, the multithreaded server path (`server.c:84-111`) and the multithreaded client pipeline could race on `io_ssl`. Furthermore, the global state makes the protocol functions non-reentrant and harder to unit test. ## Location - `src/shared/protocol.c:13` — `static SSL* io_ssl = NULL;` should be `static __thread SSL* io_ssl = NULL;`. - `src/shared/protocol.c:11-17` — all I/O state is global. ## Suggested fix 1. Make `io_ssl` thread-local, or better: 2. Refactor `send_n_data` / `receive_n_data` to take an explicit I/O context struct (containing read fd, write fd, SSL pointer, and bandwidth limit) instead of using global state. ## Severity Medium ## Category bug, quality --- _This issue was automatically generated by the issue-creator agent._
TapTap added the bugqualityneeds-triagethreading labels 2026-07-29 18:36:29 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: TapTap/FastSync#165