pgbus 1.2.0 → 1.2.1

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: b576600eebd0ca578fe664c381b35eb41cee835c1e3fcb87b8ed4e32985d0ddc
4
- data.tar.gz: 3a1ff0eeb83c9d073c3724c4bd80c0e858062ca5c052548b743b759252055796
3
+ metadata.gz: b83154b74657408b748302e5a519dc279873003591e6eb26fd7058cc77a2ada5
4
+ data.tar.gz: 544a264d3f12999b25ae3ac6d7b000397f3441ae9bbcfd5908e19ff1b8b387e7
5
5
  SHA512:
6
- metadata.gz: 5566a1ea049e67905a314f8da7198eb25527b2f89e863967ee0f51c959fb8a7941305f33ee60c9fae8c634933f6f2d6e7519d7c798e6fec78d61dc9f3563cb7f
7
- data.tar.gz: 5bda7af1a8fe21e52072ce2f8322ef5619c56499248e9be881a5163c46ddb2bbe712e9dbd95a3aaedae3f286f25099b9826ec5a01b5283f914f21edd4951bfda
6
+ metadata.gz: fb638922520df69c08d2109578235fb4f6bd8b3b663439f3c5f7d20942e3d4b1d593695edc9cc3e1cbf1ef33cdb1cb73c25e7bd403275e73f7712b7a56cc3af7
7
+ data.tar.gz: c4a2693ed7b144d5c6966ddf2805534e0163344d36af854e957b05862bb18ceae3b217fcfd58102677b830d1a898be9fcd0932568537917050908ac14165ca0e
data/CHANGELOG.md CHANGED
@@ -8,6 +8,12 @@
8
8
 
9
9
  ### Security
10
10
 
11
+ ## [1.2.1] - 2026-11-30
12
+
13
+ ### Fixed
14
+
15
+ - **A dead-letter move no longer loses its message when the database connection drops mid-move** (mensfeld/pgmq-ruby#234). A move runs `produce` (dead-letter queue) and `delete` (source) in one PGMQ transaction. pgmq-ruby 0.10.0 gives every call inside a transaction its own `auto_reconnect` recovery: when the connection was lost between the two statements (a failover, a pooler restart, `idle_in_transaction_session_timeout`), the `delete` reset the connection and ran again on the new session, outside the transaction, and committed by itself. The dead-letter copy was rolled back with the lost session, and the move returned normally: the message was in neither queue. The same held for every `Pgbus::Client#transaction` block (the dashboard's dead-letter retry and the executor's transactional paths). A call nested in a transaction now never re-runs on its own, health-checks or resets the connection: the whole transaction runs again from `BEGIN` on a fresh connection, or, when that run fails too, raises `PGMQ::Errors::ConnectionError` with nothing committed; the message stays in its source queue and is redelivered. A connection lost while the `COMMIT` is in flight can still leave a second dead-letter copy, when the `COMMIT` landed before its reply was lost and the run from `BEGIN` produces again (at-least-once, as before). pgbus does this on the PGMQ connections it builds until a pgmq-ruby release with the fix is adopted.
16
+
11
17
  ## [1.2.0] - 2026-11-29
12
18
 
13
19
  ### Changed
@@ -24,7 +24,9 @@ module Pgbus
24
24
  # PGMQ::Errors::ConnectionError, so the executor's own completion retry
25
25
  # rescues it and reads a gone message as :ambiguous. An error from the
26
26
  # checkout health check (verify_connection!) never passes through the
27
- # block, so it stays retryable. State lives in the call's locals only.
27
+ # block, so it stays retryable. The marking state lives in the call's
28
+ # locals; the only other state is the fiber-local nested-checkout flag
29
+ # (with_connection, below).
28
30
  module MidFlightGuard
29
31
  MARKER = "Database connection lost after a statement was sent, and pgmq-ruby's re-run failed too: " \
30
32
  "the call may have committed"
@@ -38,6 +40,10 @@ module Pgbus
38
40
  # to it, so other text around them does not prove nothing was sent.
39
41
  SEND_REFUSED = /\A(?:PQ\w+(?:\(\))? )?(?:no connection to the server|connection is closed)\s*\z/i
40
42
 
43
+ # Frozen once, so the hot path allocates no option hash per call.
44
+ DEFER_INTERRUPTS = { Exception => :never }.freeze
45
+ ALLOW_INTERRUPTS = { Exception => :immediate }.freeze
46
+
41
47
  # Hooked on the Connection, not the Client: PGMQ::Client#transaction
42
48
  # checks out through @connection.with_connection directly.
43
49
  def self.install(pgmq)
@@ -45,6 +51,14 @@ module Pgbus
45
51
  pgmq
46
52
  end
47
53
 
54
+ # Fiber-local key (Thread#[] is fiber-local, the scope of the pool's own
55
+ # re-entrant checkout) marking that this fiber holds an outermost
56
+ # checkout of the connection. Set once, when the guard is installed.
57
+ def self.extended(connection)
58
+ super
59
+ connection.instance_variable_set(:@pgbus_checkout_key, :"pgbus_pgmq_checkout_#{connection.object_id}")
60
+ end
61
+
48
62
  # include?, not start_with?: PGMQ::Client#transaction re-raises as
49
63
  # "Transaction failed: <message>".
50
64
  def self.lost?(error)
@@ -52,10 +66,30 @@ module Pgbus
52
66
  error.message.include?(MARKER)
53
67
  end
54
68
 
55
- def with_connection
69
+ # A checkout nested inside this fiber's outermost one (every call inside
70
+ # PGMQ::Client#transaction) gets the outermost's connection. pgmq-ruby
71
+ # 0.10.0 gives it the full auto_reconnect recovery: after a lost
72
+ # connection it resets the connection and re-runs the call on the new
73
+ # session, outside the transaction, so the re-run and every later call
74
+ # commit on their own (mensfeld/pgmq-ruby#234: a dead-letter move lost
75
+ # its message). Here a nested checkout neither health-checks, resets nor
76
+ # re-runs; its error ends the transaction's block, pg's ROLLBACK on the
77
+ # dead connection raises a lost-connection PG::Error to the outermost
78
+ # checkout, and that one re-runs the whole transaction from BEGIN.
79
+ def with_connection(&)
80
+ return with_nested_connection(&) if Thread.current[@pgbus_checkout_key]
81
+
56
82
  mid_flight = nil
57
83
  super do |conn|
58
- yield conn
84
+ # Set and cleared with async interrupts (Timeout, Thread#raise)
85
+ # deferred, as connection_pool guards its own fiber-local keys: a
86
+ # flag left set would make every later call on this fiber nested.
87
+ Thread.handle_interrupt(DEFER_INTERRUPTS) do
88
+ Thread.current[@pgbus_checkout_key] = true
89
+ Thread.handle_interrupt(ALLOW_INTERRUPTS) { yield conn }
90
+ ensure
91
+ Thread.current[@pgbus_checkout_key] = nil
92
+ end
59
93
  rescue PG::Error => e
60
94
  mid_flight ||= e unless SEND_REFUSED.match?(e.message)
61
95
  raise
@@ -65,6 +99,16 @@ module Pgbus
65
99
 
66
100
  raise PGMQ::Errors::ConnectionError, "#{MARKER} (#{mid_flight.message.strip}); re-run: #{e.message}"
67
101
  end
102
+
103
+ private
104
+
105
+ # The pool's checkout is re-entrant per fiber, so this is the outermost
106
+ # checkout's connection. A PG::Error is wrapped as pgmq-ruby wraps it.
107
+ def with_nested_connection(&)
108
+ pool.with(&)
109
+ rescue PG::Error => e
110
+ raise PGMQ::Errors::ConnectionError, "Database connection error: #{e.message}"
111
+ end
68
112
  end
69
113
  end
70
114
  end
data/lib/pgbus/client.rb CHANGED
@@ -640,7 +640,9 @@ module Pgbus
640
640
  # attempt hits a pre-flight stale-connection error — safe because no SQL
641
641
  # was sent on the first attempt (the connection was dead before the BEGIN;
642
642
  # a run that failed after sending is marked by MidFlightGuard and not
643
- # retried).
643
+ # retried). A connection lost inside the block re-runs the whole block from
644
+ # BEGIN, never one call of it on its own (MidFlightGuard's nested
645
+ # checkouts), so it may also run twice that way: keep it safe to run twice.
644
646
  def transaction(&block)
645
647
  with_stale_connection_retry do
646
648
  synchronized { @pgmq.transaction(&block) }
data/lib/pgbus/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pgbus
4
- VERSION = "1.2.0"
4
+ VERSION = "1.2.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgbus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson