Skip to content

Commit 555d98f

Browse files
committed
change imap idle done handling
1 parent 760253f commit 555d98f

3 files changed

Lines changed: 6 additions & 6 deletions

File tree

app/services/imap/gmail_client.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,17 @@ def idle_once(timeout: 1500)
122122
begin
123123
if @imap.respond_to?(:idle)
124124
# Break IDLE promptly on first response so the caller can FETCH.
125+
# Response handlers run outside the monitor in the receiver thread,
126+
# and idle's wait releases the monitor, so idle_done can be called
127+
# directly without deadlock. Avoids a detached Thread that can
128+
# outlive the connection and hit a torn-down monitor.
125129
idle_done_called = false
126130
@imap.idle(timeout) do |resp|
127131
got_activity = true
128132
yield resp if block_given?
129133
unless idle_done_called
130134
idle_done_called = true
131-
Thread.new { @imap.idle_done }
135+
@imap.idle_done
132136
end
133137
end
134138
else

app/services/imap_idle_runner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def run(max_cycles: nil, idle_timeout: DEFAULT_IDLE_TIMEOUT)
2828
begin
2929
main_loop(max_cycles: max_cycles, idle_timeout: idle_timeout)
3030
rescue => e
31-
@logger.error("IMAP runner fatal error: #{e.class}: #{e.message}")
31+
@logger.error("IMAP runner fatal error: #{e.class}: #{e.message}\n#{e.backtrace&.first(10)&.join("\n")}")
3232
update_state(last_error: short_error(e))
3333
raise
3434
end

spec/services/imap/gmail_client_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@
4949
# Simulate Net::IMAP#idle with yielding one response
5050
allow(imap_double).to receive(:respond_to?).with(:idle).and_return(true)
5151
allow(imap_double).to receive(:idle_done)
52-
allow(Thread).to receive(:new).and_wrap_original do |m, *args, &blk|
53-
blk.call
54-
double('Thread', join: true)
55-
end
5652
yielded = []
5753
expect(imap_double).to receive(:idle) do |timeout, &blk|
5854
blk.call(double('Resp', name: 'EXISTS', data: 1))

0 commit comments

Comments
 (0)