Skip to content

Commit e3fc29a

Browse files
byrootk0kubun
authored andcommitted
Improve backtrace of errors raised by Socket.tcp_with_fast_fallback
[Bug #21211] Socket errors raised from background threads are hard to track down because their backtrace starts from the spawned thread. To solve this we can raise a new error with the old one as `cause`.
1 parent 66d133a commit e3fc29a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

ext/socket/lib/socket.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ def self.tcp_with_fast_fallback(host, port, local_host = nil, local_port = nil,
693693
connection_attempt_delay_expires_at = nil
694694
user_specified_connect_timeout_at = nil
695695
last_error = nil
696+
last_error_from_thread = false
696697

697698
if resolving_family_names.size == 1
698699
family_name = resolving_family_names.first
@@ -865,6 +866,7 @@ def self.tcp_with_fast_fallback(host, port, local_host = nil, local_port = nil,
865866
other = family_name == :ipv6 ? :ipv4 : :ipv6
866867
if !resolution_store.resolved?(other) || !resolution_store.resolved_successfully?(other)
867868
last_error = result
869+
last_error_from_thread = true
868870
end
869871
end
870872
else
@@ -885,7 +887,11 @@ def self.tcp_with_fast_fallback(host, port, local_host = nil, local_port = nil,
885887

886888
if resolution_store.empty_addrinfos?
887889
if connecting_sockets.empty? && resolution_store.resolved_all_families?
888-
raise last_error
890+
if last_error_from_thread
891+
raise last_error.class, last_error.message, cause: last_error
892+
else
893+
raise last_error
894+
end
889895
end
890896

891897
if (expired?(now, user_specified_resolv_timeout_at) || resolution_store.resolved_all_families?) &&

0 commit comments

Comments
 (0)