Skip to content

Fix thread-safety issue in HTTP connection handling#31

Merged
oriolgual merged 3 commits intomainfrom
fix-thread-safe-http
Nov 27, 2025
Merged

Fix thread-safety issue in HTTP connection handling#31
oriolgual merged 3 commits intomainfrom
fix-thread-safe-http

Conversation

@MiguelPF
Copy link
Contributor

Remove memoization of Net::HTTP instance to prevent frozen SSL context errors when using concurrent requests. Each request now creates its own HTTP connection, ensuring thread-safety when using libraries like concurrent-ruby.

This fixes the 'FrozenError: can't modify frozen OpenSSL::SSL::SSLContext' error that occurs when multiple threads try to use the same HTTP connection simultaneously.

The overhead of creating a new HTTP connection per request is negligible compared to the actual API call time (which typically takes seconds).

Remove memoization of Net::HTTP instance to prevent frozen SSL context
errors when using concurrent requests. Each request now creates its own
HTTP connection, ensuring thread-safety when using libraries like
concurrent-ruby.

This fixes the 'FrozenError: can't modify frozen OpenSSL::SSL::SSLContext'
error that occurs when multiple threads try to use the same HTTP connection
simultaneously.

The overhead of creating a new HTTP connection per request is negligible
compared to the actual API call time (which typically takes seconds).
@MiguelPF MiguelPF requested a review from a team as a code owner November 26, 2025 15:27
@MiguelPF MiguelPF requested a review from oriolgual November 26, 2025 19:37
The method name 'http' was misleading as it suggested a memoized getter,
but the implementation creates a fresh Net::HTTP instance on each call.
Renaming to 'build_http' makes this factory pattern explicit and clarifies
that each call creates a new connection with its own SSL context for thread-safety.
@MiguelPF MiguelPF self-assigned this Nov 27, 2025
@MiguelPF MiguelPF added the bug Something isn't working label Nov 27, 2025
Makes the factory pattern more explicit by storing the HTTP instance
in a local variable before using it. This improves code clarity and
makes it obvious that each method gets its own connection instance.
@oriolgual oriolgual merged commit 33db9fa into main Nov 27, 2025
1 check passed
@oriolgual oriolgual deleted the fix-thread-safe-http branch November 27, 2025 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants