Skip to content

Commit 286f757

Browse files
committed
♻️ Don't wait for lock _before_ disconnecting
Net::IMAP#disconnect will still attempt to enter the logout state first, but it won't wait for the lock until after it's shutdown the socket.
1 parent e600e7b commit 286f757

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

lib/net/imap.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ def tls_verified?; @tls_verified end
11161116
#
11171117
# Related: #logout, #logout!
11181118
def disconnect
1119-
state_logout! unless connection_state.to_sym == :logout
1119+
in_logout_state = try_state_logout?
11201120
return if disconnected?
11211121
begin
11221122
begin
@@ -1136,6 +1136,10 @@ def disconnect
11361136
@sock.close
11371137
end
11381138
raise e if e
1139+
ensure
1140+
# Try again after shutting down the receiver thread. With no reciever
1141+
# left to wait for, any remaining locks should be _very_ brief.
1142+
state_logout! unless in_logout_state
11391143
end
11401144

11411145
# Returns true if disconnected from the server.
@@ -3804,6 +3808,16 @@ def state_logout!
38043808
end
38053809
end
38063810

3811+
# don't wait to aqcuire the lock
3812+
def try_state_logout?
3813+
return true if connection_state in [:logout, *]
3814+
return false unless acquired_lock = mon_try_enter
3815+
state_logout!
3816+
true
3817+
ensure
3818+
mon_exit if acquired_lock
3819+
end
3820+
38073821
def sasl_adapter
38083822
SASLAdapter.new(self, &method(:send_command_with_continuations))
38093823
end

0 commit comments

Comments
 (0)