omq 0.30.1 → 0.30.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cbb4225bd58846cc3ec28c008f11a4da2a3d0073be24ced5d6f4e99873b9cf91
4
- data.tar.gz: 7e11730e874f7c24e4207d661c7e4efceb117b68c31b823b4cabdb5367cbd787
3
+ metadata.gz: a3573065b572ff83a1392b990994e9d7900aef3961dded7a5eddc9b5f4812734
4
+ data.tar.gz: 526b831d065817c9e9609232e96642b84b33d3e380cc872f69a7d888999b6ddd
5
5
  SHA512:
6
- metadata.gz: e27866ec7266e32cee10ed0b70a651d9466018a9900b1754b32697962b845bb6abda645d36c307133739bee41536d99ac1cbf5f7156b02a32122b86b2e484b8a
7
- data.tar.gz: 72970544ae442b3bf35e4ad95800d3d199ec6e72d376ce5fd8f573317e7e10bda51b2154bb68777c50a52e2df8e170756e1ff07e9b1e20cbe84012937b616956
6
+ metadata.gz: 4096b089eb2083c9cad686cd538e63d42ae1a5b47c3bc7c389693d651880cd4b6d7184606f68f8cfab6f7ee1d50fc5465996ae2c16e9368f0d34bdfd6438976e
7
+ data.tar.gz: 1d2d753415f87a2210687d016b4c447c6c8651c9b8ae8494ce89415c92b626acbab9ddde8877543d94943a4d3f9731da93565da423a7d04ddd6900894ca0118c
data/CHANGELOG.md CHANGED
@@ -2,6 +2,37 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.30.3] - 2026-12-03
6
+
7
+ ### Fixed
8
+
9
+ - `#close` with linger keeps a TCP or IPC connection open after draining,
10
+ reading until the peer closes too, for the rest of the linger (10 s
11
+ without a limit). Closing right away made bytes the peer still sent, such
12
+ as a heartbeat PING, reset the connection, and the kernel discarded what
13
+ it hadn't sent yet. Requires protocol-zmtp 0.11.1 for
14
+ `Connection#linger_close`.
15
+ - A `#receive` outside Async that is interrupted (`Interrupt`,
16
+ `Thread#raise`, `Timeout.timeout`, `Thread#kill`) no longer loses the next
17
+ message. Its work on the IO thread kept running and took that message for
18
+ a caller that was gone. It is now stopped before it consumes anything, and
19
+ a message it already took is returned by the next `#receive`.
20
+ - The process no longer hangs at exit while a thread outside Async is
21
+ blocked in `#receive` or `#send`. `Reactor.stop!` waited on that work
22
+ forever with the default infinite linger. On stop, a blocked `#receive`
23
+ now returns `nil` and a blocked `#send` raises `SocketClosedError`, as on
24
+ a closed socket.
25
+
26
+ ## [0.30.2] - 2026-12-02
27
+
28
+ ### Fixed
29
+
30
+ - A `tcp://` dial to a free local port can get that same port as its source
31
+ and connect to itself (zeromq/libzmq#4958). The dialer now closes that
32
+ socket and retries as if refused. Before, PAIR, DEALER, ROUTER, CHANNEL
33
+ and PEER sockets completed the handshake with themselves and stayed
34
+ connected to themselves.
35
+
5
36
  ## [0.30.1] - 2026-11-27
6
37
 
7
38
  ### Added
@@ -253,7 +253,15 @@ module OMQ
253
253
  @engine.connections.delete(@conn)
254
254
  @engine.routing.connection_removed(@conn) if @conn
255
255
  @engine.wake_drain
256
- @conn&.close rescue nil
256
+
257
+ # A graceful close keeps the socket open until the peer has read
258
+ # what we sent. The pumps stop first, so nothing else reads it.
259
+ linger = @engine.linger_close_timeout if @conn.respond_to?(:linger_close)
260
+
261
+ unless linger
262
+ @conn&.close rescue nil
263
+ end
264
+
257
265
  detail = reason ? { error: reason, reason: reason.message } : nil
258
266
  @engine.emit_monitor_event(:disconnected, endpoint: @endpoint, detail: detail)
259
267
  @done&.resolve(true)
@@ -269,6 +277,8 @@ module OMQ
269
277
  # the supervisor task, which is NOT in the barrier — so there
270
278
  # is no self-stop risk.
271
279
  @barrier.stop
280
+
281
+ @conn.linger_close(linger) if linger
272
282
  end
273
283
 
274
284
 
data/lib/omq/engine.rb CHANGED
@@ -24,6 +24,11 @@ module OMQ
24
24
  include PumpTasks
25
25
 
26
26
 
27
+ # Seconds a connection closing without a linger limit waits for its
28
+ # peer to read what we sent, as in omq.rs.
29
+ PEER_CLOSE_TIMEOUT = 10
30
+
31
+
27
32
  # Scheme → transport module registry.
28
33
  # Plugins add entries via +Engine.transports["scheme"] = MyTransport+.
29
34
  #
@@ -82,6 +87,12 @@ module OMQ
82
87
  attr_reader :lifecycle
83
88
 
84
89
 
90
+ # @return [Numeric, nil] seconds a gracefully closing connection waits
91
+ # for its peer's EOF, set once {#close} has drained the send queues
92
+ #
93
+ attr_reader :linger_close_timeout
94
+
95
+
85
96
  # @!attribute [w] monitor_queue
86
97
  # @param value [MonitorQueue, nil] queue for monitor events
87
98
  #
@@ -118,6 +129,8 @@ module OMQ
118
129
  @verbose_monitor = false
119
130
  @drain_signal = nil # set while #close waits for send queues
120
131
  @batches_sending = 0 # batches pumps took off a queue and haven't flushed
132
+
133
+ @linger_close_timeout = nil
121
134
  end
122
135
 
123
136
 
@@ -408,6 +421,7 @@ module OMQ
408
421
  #
409
422
  def dequeue_recv
410
423
  raise @fatal_error if @fatal_error
424
+ return @requeued.shift if @requeued && !@requeued.empty?
411
425
  return if @lifecycle.closed?
412
426
 
413
427
  msg = routing.dequeue_recv
@@ -417,6 +431,16 @@ module OMQ
417
431
  end
418
432
 
419
433
 
434
+ # Hands back a message whose cross-thread receiver gave up after it
435
+ # was dequeued (see {Reactor.run}). The next {#dequeue_recv} returns it.
436
+ #
437
+ # @param msg [Array<String>]
438
+ #
439
+ def requeue_recv(msg)
440
+ (@requeued ||= []) << msg
441
+ end
442
+
443
+
420
444
  # Pushes a nil sentinel into the recv queue, unblocking a
421
445
  # pending {#dequeue_recv} with a nil return value.
422
446
  #
@@ -503,7 +527,11 @@ module OMQ
503
527
  stop_listeners unless @connections.empty?
504
528
 
505
529
  if @options.linger.nil? || @options.linger > 0
530
+ started = Async::Clock.now
531
+
506
532
  drain_send_queues(@options.linger)
533
+
534
+ @linger_close_timeout = remaining_linger(started)
507
535
  end
508
536
 
509
537
  @lifecycle.finish_closing!
@@ -760,6 +788,21 @@ module OMQ
760
788
  end
761
789
 
762
790
 
791
+ # The rest of the linger after the send queues drained, or
792
+ # PEER_CLOSE_TIMEOUT without a limit.
793
+ #
794
+ # @param started [Float] when {#close} started draining
795
+ # @return [Numeric]
796
+ #
797
+ def remaining_linger(started)
798
+ linger = @options.linger
799
+
800
+ return PEER_CLOSE_TIMEOUT unless linger&.finite?
801
+
802
+ [linger - (Async::Clock.now - started), 0].max
803
+ end
804
+
805
+
763
806
  # Wakes fibers blocked in a send (they raise {SocketClosedError}) or
764
807
  # a receive (they get +nil+).
765
808
  #
@@ -16,7 +16,7 @@ module OMQ
16
16
  # @raise [IO::TimeoutError] if timeout exceeded
17
17
  #
18
18
  def dequeue(timeout: @options.read_timeout)
19
- Reactor.run(timeout:) { @engine.dequeue_recv }
19
+ Reactor.run(timeout:, requeue_to: @engine) { @engine.dequeue_recv }
20
20
  end
21
21
 
22
22
 
data/lib/omq/reactor.rb CHANGED
@@ -16,6 +16,34 @@ module OMQ
16
16
  # thread are dispatched to the IO thread via {.run}.
17
17
  #
18
18
  module Reactor
19
+ # A block dispatched from a non-Async thread to the IO thread.
20
+ #
21
+ # @!attribute [r] requeue_to
22
+ # @return [#requeue_recv, nil] set for receives: takes back a
23
+ # message whose caller gave up after it was dequeued
24
+ #
25
+ class Job
26
+ attr_reader :block, :timeout, :requeue_to, :promise
27
+ attr_accessor :task
28
+
29
+
30
+ def initialize(block, timeout, requeue_to)
31
+ @block = block
32
+ @timeout = timeout
33
+ @requeue_to = requeue_to
34
+ @promise = Async::Promise.new
35
+ @task = nil
36
+ end
37
+
38
+ end
39
+
40
+
41
+ # Sent by a caller that stopped waiting for its {Job} (e.g. on
42
+ # +Interrupt+ or +Thread#raise+).
43
+ #
44
+ Abandon = Data.define(:job)
45
+
46
+
19
47
  THREAD_NAME = 'omq-io'
20
48
  NATIVE_FIBER_SCHEDULER = RUBY_ENGINE != "jruby" &&
21
49
  Fiber.respond_to?(:scheduler) &&
@@ -72,11 +100,18 @@ module OMQ
72
100
  #
73
101
  # Inside an Async reactor: runs directly.
74
102
  # Outside: dispatches to the shared IO thread and blocks
75
- # the calling thread until the result is available.
103
+ # the calling thread until the result is available. If the
104
+ # calling thread stops waiting (+Interrupt+, +Thread#raise+,
105
+ # +Thread#kill+), the dispatched work is cancelled before it
106
+ # consumes anything; a message it already dequeued goes back to
107
+ # +requeue_to+.
76
108
  #
109
+ # @param timeout [Numeric, nil] timeout in seconds
110
+ # @param requeue_to [#requeue_recv, nil] marks the block as a
111
+ # receive (see {Job#requeue_to})
77
112
  # @return [Object] the block's return value
78
113
  #
79
- def run(timeout: nil, &block)
114
+ def run(timeout: nil, requeue_to: nil, &block)
80
115
  task = Async::Task.current?
81
116
 
82
117
  if task
@@ -92,10 +127,10 @@ module OMQ
92
127
  yield
93
128
  end
94
129
  else
95
- result = Async::Promise.new
130
+ job = Job.new(block, timeout, requeue_to)
96
131
  root_task # ensure started
97
- @work_queue << [block, result, timeout]
98
- result.wait
132
+ enqueue(job)
133
+ wait_for(job)
99
134
  end
100
135
  end
101
136
 
@@ -161,31 +196,140 @@ module OMQ
161
196
  end
162
197
 
163
198
 
199
+ # Hands a work item to the IO thread. If the reactor is shutting
200
+ # down, a {Job} is settled as stopped instead.
201
+ #
202
+ # @param item [Job, Abandon]
203
+ #
204
+ def enqueue(item)
205
+ queue = @work_queue
206
+ raise Async::Queue::ClosedError unless queue
207
+
208
+ queue << item
209
+ rescue Async::Queue::ClosedError
210
+ settle_stopped(item) if item.is_a?(Job)
211
+ end
212
+
213
+
214
+ # Settles a job that won't run to completion because the reactor
215
+ # stops: receives get +nil+ (like a closed socket), anything else
216
+ # raises {SocketClosedError}.
217
+ #
218
+ # @param job [Job]
219
+ #
220
+ def settle_stopped(job)
221
+ if job.requeue_to
222
+ job.promise.resolve(nil)
223
+ else
224
+ job.promise.reject(SocketClosedError.new("OMQ reactor stopped"))
225
+ end
226
+ end
227
+
228
+
229
+ # Waits for a dispatched job. If the caller stops waiting
230
+ # (+Interrupt+, +Thread#raise+, +Thread#kill+), the IO thread is
231
+ # told to cancel the job or hand back its result. Only that rare
232
+ # path masks interrupts: +Thread.handle_interrupt+ on every call
233
+ # costs extra GVL handoffs.
234
+ #
235
+ # @param job [Job]
236
+ # @return [Object] the job's value
237
+ #
238
+ def wait_for(job)
239
+ returned = false
240
+ value = job.promise.wait
241
+ returned = true
242
+ value
243
+ ensure
244
+ unless returned || job.promise.failed?
245
+ Thread.handle_interrupt(Object => :never) { enqueue(Abandon.new(job)) }
246
+ end
247
+ end
248
+
249
+
164
250
  # Runs the shared Async reactor.
165
251
  #
166
252
  # Processes work items dispatched via {.run} while engine
167
253
  # tasks (accept loops, pumps, etc.) run as transient children.
254
+ # On shutdown, stops jobs still blocked so their threads wake up
255
+ # and the reactor can exit.
168
256
  #
169
257
  # @param ready [Thread::Queue] receives the root task once started
170
258
  #
171
259
  def run_reactor(ready)
260
+ queue = @work_queue
261
+
172
262
  Async do |task|
263
+ running = Set.new
173
264
  ready.push(task)
174
265
 
175
- loop do
176
- item = @work_queue.dequeue or break
177
- block, result, timeout = item
178
-
179
- task.async do |t|
180
- if timeout
181
- result.fulfill do
182
- t.with_timeout(timeout, IO::TimeoutError) { block.call }
183
- end
184
- else
185
- result.fulfill { block.call }
266
+ begin
267
+ while (item = queue.dequeue)
268
+ case item
269
+ when Job
270
+ start_job(item, task, running)
271
+ when Abandon
272
+ abandon_job(item.job)
186
273
  end
187
274
  end
275
+ ensure
276
+ queue.close
277
+
278
+ # Jobs enqueued after the stop signal never start.
279
+ while (item = queue.dequeue)
280
+ settle_stopped(item) if item.is_a?(Job)
281
+ end
282
+
283
+ running.to_a.each { |job| job.task.stop }
284
+ end
285
+ end
286
+ end
287
+
288
+
289
+ # Runs a job's block in its own task. The job is in +running+
290
+ # until its task finishes.
291
+ #
292
+ # @param job [Job]
293
+ # @param parent [Async::Task]
294
+ # @param running [Set<Job>]
295
+ #
296
+ def start_job(job, parent, running)
297
+ running << job
298
+
299
+ job.task = parent.async do |t|
300
+ value = if job.timeout
301
+ t.with_timeout(job.timeout, IO::TimeoutError) { job.block.call }
302
+ else
303
+ job.block.call
304
+ end
305
+
306
+ job.promise.resolve(value)
307
+ rescue Async::Stop
308
+ settle_stopped(job)
309
+ rescue Exception => error
310
+ job.promise.reject(error)
311
+ ensure
312
+ running.delete(job)
313
+ end
314
+ end
315
+
316
+
317
+ # Handles a job whose caller stopped waiting. A job still
318
+ # blocked is stopped before it consumes anything. A receive that
319
+ # already completed hands its message back, so the next receive
320
+ # returns it.
321
+ #
322
+ # @param job [Job]
323
+ #
324
+ def abandon_job(job)
325
+ if job.promise.resolved?
326
+ value = job.promise.value
327
+
328
+ if job.promise.completed? && value && job.requeue_to.respond_to?(:requeue_recv)
329
+ job.requeue_to.requeue_recv(value)
188
330
  end
331
+ else
332
+ job.task&.stop
189
333
  end
190
334
  end
191
335
 
data/lib/omq/readable.rb CHANGED
@@ -15,7 +15,7 @@ module OMQ
15
15
  #
16
16
  def receive
17
17
  if @engine.on_io_thread?
18
- Reactor.run(timeout: @options.read_timeout) { @engine.dequeue_recv }
18
+ Reactor.run(timeout: @options.read_timeout, requeue_to: @engine) { @engine.dequeue_recv }
19
19
  elsif (timeout = @options.read_timeout)
20
20
  if Reactor.native_fiber_scheduler?
21
21
  Async::Task.current.with_timeout(timeout, IO::TimeoutError) { @engine.dequeue_recv }
@@ -147,6 +147,31 @@ module OMQ
147
147
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVBUF, options.rcvbuf)
148
148
  end
149
149
  end
150
+
151
+
152
+ # A dial to a free local port can get that same port as its source
153
+ # and connect to itself (TCP simultaneous open). The handshake would
154
+ # then run against the socket itself. Nothing listens on that port,
155
+ # so this closes +sock+ and raises as refused, and the dialer retries.
156
+ #
157
+ # @param sock [Socket] a dialed socket
158
+ # @return [void]
159
+ # @raise [Errno::ECONNREFUSED] if +sock+ is connected to itself
160
+ #
161
+ def reject_self_connect!(sock)
162
+ local = sock.local_address
163
+ remote = sock.remote_address
164
+
165
+ return unless local.ip_unpack == remote.ip_unpack
166
+
167
+ begin
168
+ sock.close
169
+ rescue SystemCallError
170
+ # A failed close must not hide the refusal the dialer retries on.
171
+ end
172
+
173
+ raise Errno::ECONNREFUSED, "#{remote.inspect_sockaddr} connected to itself"
174
+ end
150
175
  end
151
176
 
152
177
 
@@ -179,7 +204,10 @@ module OMQ
179
204
  host, port = TCP.parse_endpoint(@endpoint)
180
205
  host = TCP.normalize_connect_host(host)
181
206
  sock = ::Socket.tcp(host, port, connect_timeout: TCP.connect_timeout(@engine.options))
207
+
208
+ TCP.reject_self_connect!(sock)
182
209
  TCP.apply_buffer_sizes(sock, @engine.options)
210
+
183
211
  @engine.handle_connected(IO::Stream::Buffered.wrap(sock), endpoint: @endpoint)
184
212
  end
185
213
 
data/lib/omq/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OMQ
4
- VERSION = "0.30.1"
4
+ VERSION = "0.30.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.30.1
4
+ version: 0.30.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrik Wenger
@@ -16,6 +16,9 @@ dependencies:
16
16
  - - "~>"
17
17
  - !ruby/object:Gem::Version
18
18
  version: '0.11'
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: 0.11.1
19
22
  type: :runtime
20
23
  prerelease: false
21
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -23,6 +26,9 @@ dependencies:
23
26
  - - "~>"
24
27
  - !ruby/object:Gem::Version
25
28
  version: '0.11'
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 0.11.1
26
32
  - !ruby/object:Gem::Dependency
27
33
  name: async
28
34
  requirement: !ruby/object:Gem::Requirement