bidi2pdf 0.1.23 → 0.1.24

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: 9bff5f993e54cb0f70e6a2c83e022bf29fdef7b0fafc6ce8b641d5ec33aea7ea
4
- data.tar.gz: e2466bb66703e3a7ac4ca16bfdea22f836c8fff07492e9f44904ecf5bc9c9c63
3
+ metadata.gz: 924c1d8c78d46fdb5e08e333861afbac5384a17b3d4a78c365f9cd09d88b5068
4
+ data.tar.gz: 973fcae887672a67d00acb6ee131866ef904f2b6f67661f180b7e51f16b90c3f
5
5
  SHA512:
6
- metadata.gz: 6958ef3cc24d5350fdb090598940410f5cd3ab9abef4003f118d52ab61142b45c6427e0225ed4d37a9fa3a23d0dfd033e67a07cc981f891cb2f1f95f7af67612
7
- data.tar.gz: 74a366dc1c4953bb6e17e8c7578d3ec79cd7a0ea9f1f058f9d353ebdab12b2b046d7e25424b22745081895b09f60a5ca7a701eb29763472b9320e6a564142ad7
6
+ metadata.gz: f73a0fddac84bfc084e57a2a5353587d00d72ad9dd9f37d6ec35ce86050794b033fed60a7ed8d7425ee5624b9a1757941e8f646b1155057f171f704c2799811e
7
+ data.tar.gz: 96dcbd832b570bc9723581cffc4eeae40bb547eb7eb9ca81578911ea0985953c3353b7ac4c8896cfd490240862b48d88aade7d38e4cb788a2c23058ee20350d0
data/CHANGELOG.md CHANGED
@@ -8,10 +8,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8
8
 
9
9
  ## [Unreleased]
10
10
 
11
- [unreleased]: https://github.com/dieter-medium/bidi2pdf/compare/v0.1.23..HEAD
11
+ [unreleased]: https://github.com/dieter-medium/bidi2pdf/compare/v0.1.24..HEAD
12
12
 
13
13
  <!-- generated by git-cliff end -->
14
14
 
15
+ ## [0.1.24] - 2026-10-27
16
+
17
+ ### 🐛 Fixed
18
+ - Release every live slot after a fork
19
+ - Release a forked child's in-flight slots too
20
+ - Close a forked child's copies of inherited sockets
21
+ - Give a forked child its own session warmer
22
+
23
+ ### 🔄 Changed
24
+ - Merge pull request #174 from dieter-medium/fix/warmer-after-fork
25
+ - Merge pull request #173 from dieter-medium/dependabot/bundler/main/rubocop-1.92.1
26
+
27
+ ### 🔧 Build
28
+ - Bump rubocop from 1.92.0 to 1.92.1
29
+
15
30
  ## [0.1.23] - 2026-10-18
16
31
 
17
32
  ### 🐛 Fixed
@@ -596,7 +611,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
596
611
 
597
612
  ### 🔄 Released
598
613
 
599
- - [unreleased](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.23..HEAD)
614
+ - [unreleased](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.24..HEAD)
615
+ - [0.1.24](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.23..v0.1.24)
600
616
  - [0.1.23](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.22..v0.1.23)
601
617
  - [0.1.22](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.21..v0.1.22)
602
618
  - [0.1.21](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.20..v0.1.21)
data/README.md CHANGED
@@ -825,6 +825,61 @@ session becomes a leftover like any other. Only processes that share the registr
825
825
  each other's leases: give job workers in another container the same `registry_dir` on a shared
826
826
  volume. Sessions other tools opened have no lease; under `scope: :all` only `min_age` protects them.
827
827
 
828
+ **Forked processes (Puma workers).** The warmer belongs to the process that configured it. A child
829
+ forked after that - Rails boots the app, and with it the warmer, in Puma's master before it forks its
830
+ workers - does not use the copy it inherits: its threads did not survive the fork, and its slots,
831
+ WebSocket connections included, are the parent's (a render on one used to time out while the parent
832
+ took the answers). The child drops that copy - it closes only its own copies of the spare slots'
833
+ sockets, locally, with no WebSocket close frame and no session end, so nothing of its parent's is
834
+ touched - and builds its own warmer. The parent keeps its warmer.
835
+
836
+ Without further setup the child builds it on its first render, and that render then waits for the
837
+ whole warm-up: `size` new Chrome sessions, one after the other. To warm each worker before it takes
838
+ requests:
839
+
840
+ ```ruby
841
+ # config/puma.rb
842
+ on_worker_boot do
843
+ Bidi2pdf::SessionWarmer.after_fork
844
+ end
845
+ ```
846
+
847
+ Every worker then holds `size` spares of its own, besides the master's: with `W` workers, `size * (W + 1)`
848
+ idle sessions after boot - 2 workers at `size = 1` keep 3 Chromes waiting - and during renders one more
849
+ per render in flight plus the replacements warming. Count them in when sizing the chromedriver
850
+ (`max_sessions`, its memory and its process limit).
851
+
852
+ Puma's master renders nothing. To keep it from holding warm sessions nobody uses, end its warmer before
853
+ it forks - at that point no worker exists yet, so no child holds a copy of those sessions:
854
+
855
+ ```ruby
856
+ # config/puma.rb
857
+ before_fork do
858
+ Bidi2pdf::SessionWarmer.shutdown
859
+ end
860
+ ```
861
+
862
+ With `fork_worker`, worker 0 serves requests and forks the other workers itself; `before_fork` runs only
863
+ in the master and does not cover those forks. Puma runs `before_refork` in worker 0 before it forks and
864
+ `after_refork` once the cycle is done, so end worker 0's warmer before and give it a new one after:
865
+
866
+ ```ruby
867
+ # config/puma.rb, with fork_worker
868
+ before_refork do
869
+ Bidi2pdf::SessionWarmer.shutdown
870
+ end
871
+
872
+ after_refork do
873
+ Bidi2pdf::SessionWarmer.after_fork # builds worker 0 a warmer of its own again
874
+ end
875
+ ```
876
+
877
+ The workers worker 0 forks run `on_worker_boot` like any other. A render still in flight in worker 0 when
878
+ it forks is fine: a child releases every slot it inherited - warm, out for a render, or being checked out
879
+ or retired - and a release that fails is logged while the others go on. Not covered is a session still
880
+ being built in the forking process at that instant: its socket copy in the child is left to garbage
881
+ collection (a file descriptor, nothing the child or the parent uses).
882
+
828
883
  **Last resort: pressure.** `sweep!(pressure: true)` closes every session nobody holds that is past
829
884
  `min_age`, without waiting for `orphan_age`, the unresponsive checks or the limit. `with_retry` does
830
885
  that when its block fails with a resource error - chromedriver refusing a session
@@ -82,6 +82,23 @@ module Bidi2pdf
82
82
  end
83
83
  end
84
84
 
85
+ # In a forked child: drops this process's copy of a connection the parent opened, without a word
86
+ # to the peer - no close frame, no TLS close_notify (the underlying TCP socket is closed, not the
87
+ # SSL one), no :close event - so the parent goes on using it. Closing a descriptor the parent also
88
+ # holds ends nothing for the peer; the reader thread did not survive the fork.
89
+ def release_after_fork
90
+ @close_monitor.synchronize do
91
+ return if @closed
92
+
93
+ @closed = true
94
+ io = @socket.respond_to?(:to_io) ? @socket.to_io : @socket
95
+ io&.close unless io.nil? || io.closed?
96
+ @socket = nil
97
+ end
98
+ rescue IOError, SystemCallError
99
+ nil
100
+ end
101
+
85
102
  def open? = @handshaked && !@closed
86
103
 
87
104
  def closed? = @closed
@@ -154,6 +154,12 @@ module Bidi2pdf
154
154
  end
155
155
 
156
156
  # Closes the WebSocket connection.
157
+ # In a forked child: lets go of the parent's connection locally (BufferedWebSocketClient#release_after_fork).
158
+ def release_after_fork
159
+ @socket&.release_after_fork
160
+ @socket = nil
161
+ end
162
+
157
163
  def close
158
164
  return unless @socket
159
165
 
@@ -167,6 +167,14 @@ module Bidi2pdf
167
167
  end
168
168
  end
169
169
 
170
+ # In a forked child: drops this process's copies of the parent's connections - the command client
171
+ # and the event socket - without ending the session, which is the parent's (see #close for that).
172
+ def release_after_fork
173
+ @client&.release_after_fork
174
+ @event_socket&.release_after_fork
175
+ @client = @event_socket = nil
176
+ end
177
+
170
178
  private
171
179
 
172
180
  # Sends a command to the WebSocket client.
@@ -157,11 +157,13 @@ module Bidi2pdf
157
157
  def configure
158
158
  @config = Configuration.new
159
159
  yield @config if block_given?
160
+ abandon_inherited_instance
160
161
  @instance&.shutdown
161
162
  # Cleared first: if the constructor below raises, a stale, already-shut-down instance must
162
163
  # not stay registered (it would keep serving cold slots but never warm again).
163
164
  @instance = nil
164
165
  @instance = new(@config, slot_factory: @config.slot_factory)
166
+ @instance_pid = Process.pid
165
167
  end
166
168
 
167
169
  # Returns the warmer's configuration, initializing defaults if needed.
@@ -197,9 +199,18 @@ module Bidi2pdf
197
199
  Bidi2pdf.logger.warn "session_warmer: error closing #{label}: #{e.message}"
198
200
  end
199
201
 
200
- # Returns the shared singleton warmer instance, creating it (and pre-warming it) on first call.
202
+ # Returns the shared singleton warmer instance, creating it (and pre-warming it) on first call -
203
+ # and in a forked child, its own one instead of the copy it inherited (see
204
+ # #abandon_inherited_instance).
201
205
  def instance
202
- @instance ||= new(config, slot_factory: config.slot_factory)
206
+ abandon_inherited_instance
207
+ @instance ||= new(config, slot_factory: config.slot_factory).tap { @instance_pid = Process.pid }
208
+ end
209
+
210
+ # Warms this process's own slots now - for a forked child (a Puma worker's on_worker_boot), so
211
+ # its first render does not pay for them. Without it the child warms on its first render.
212
+ def after_fork
213
+ instance
203
214
  end
204
215
 
205
216
  # Checks out a slot, yields a fresh tab for one render, then retires the slot.
@@ -210,17 +221,37 @@ module Bidi2pdf
210
221
  # Sweeps the remote chromedriver with the configured sweeper; nil when there is none (or no
211
222
  # warmer yet - this never creates one).
212
223
  def sweep!(reason: :manual)
224
+ abandon_inherited_instance
213
225
  @instance&.sweep!(reason: reason)
214
226
  end
215
227
 
216
- # Retires every currently-warm spare and resets the singleton.
228
+ # Retires every currently-warm spare and resets the singleton. In a forked child it only drops
229
+ # the inherited copy: those spares are the parent's.
217
230
  def shutdown
231
+ abandon_inherited_instance
218
232
  @instance&.shutdown
219
233
  @instance = nil
220
234
  end
221
235
 
222
236
  private
223
237
 
238
+ # The instance belongs to the process that created it. A forked child (a Puma worker - Rails
239
+ # boots the app, and with it this warmer, in the master before the fork) inherits a copy whose
240
+ # threads did not survive the fork and whose slots are its parent's, WebSocket connections
241
+ # included: a render on one sent its commands down the parent's socket, the parent's reader
242
+ # thread took the answers ("Unknown response"), and the child timed out. So the child lets go
243
+ # of its copy - closing only its own copies of the spare slots' sockets, never their sessions,
244
+ # which are its parent's (#release_after_fork) - and builds its own from the same
245
+ # configuration.
246
+ def abandon_inherited_instance
247
+ return if @instance.nil? || @instance_pid == Process.pid
248
+
249
+ inherited = @instance
250
+ @instance = nil
251
+ @instance_pid = nil
252
+ inherited.release_after_fork
253
+ end
254
+
224
255
  # Records each part in +parts+ the moment it exists, so #default_slot_factory's rescue can
225
256
  # retire exactly what was created so far. Mirrors Launcher#session's local/remote branch.
226
257
  def build_slot(config, parts)
@@ -250,6 +281,9 @@ module Bidi2pdf
250
281
  @slot_factory = slot_factory || self.class.default_slot_factory(config)
251
282
  @mutex = Mutex.new
252
283
  @available = []
284
+ # Every slot from its creation to its retirement - warm, out for a render, or in between - so a
285
+ # forked child can let go of all of them (#release_after_fork).
286
+ @live = []
253
287
  @replenish_threads = []
254
288
  @warming = 0
255
289
  @shutdown = false
@@ -262,6 +296,29 @@ module Bidi2pdf
262
296
  start_background_work
263
297
  end
264
298
 
299
+ # In a forked child, for the copy it inherited: closes the child's copies of the sockets of every
300
+ # slot the parent had - warm, out for a render, or between the two (being checked out, being
301
+ # retired) - locally (Session#release_after_fork: no close frame, no session end), and forgets the
302
+ # slots. The parent's sessions, its renders in flight, its leases and its threads are untouched.
303
+ # One slot that fails to release does not stop the others. The warmer itself is unusable
304
+ # afterwards - .instance builds the child its own.
305
+ #
306
+ # Not covered: a session still being built when the parent forked - between chromedriver's
307
+ # answer and #create_slot listing it, a fraction of a second. Its socket copy in the child is left
308
+ # to garbage collection. That needs a fork from a process that is building a slot on another
309
+ # thread at that moment; Puma's master never renders, so its forks do not meet it.
310
+ def release_after_fork
311
+ inherited = @live.dup
312
+ @available = []
313
+ @live = []
314
+ inherited.each do |slot|
315
+ session = slot[:session]
316
+ session.release_after_fork if session.respond_to?(:release_after_fork)
317
+ rescue StandardError => e
318
+ Bidi2pdf.logger.warn "session_warmer: releasing an inherited slot failed: #{e.message}"
319
+ end
320
+ end
321
+
265
322
  # Sweeps the remote chromedriver now - for an application that suspects leaked sessions.
266
323
  #
267
324
  # @return [ChromeSweeper::Result, nil] nil when no sweeper is configured.
@@ -381,7 +438,10 @@ module Bidi2pdf
381
438
  slot = new_slot
382
439
  id = session_id_of(slot)
383
440
  @registry&.hold(id)
384
- @mutex.synchronize { @own_sessions << id } if id
441
+ @mutex.synchronize do
442
+ @own_sessions << id if id
443
+ @live << slot
444
+ end
385
445
  slot
386
446
  end
387
447
 
@@ -563,7 +623,10 @@ module Bidi2pdf
563
623
  self.class.retire_slot(session: slot[:session], manager: slot[:manager])
564
624
  id = session_id_of(slot)
565
625
  @registry&.forget(id)
566
- @mutex.synchronize { @own_sessions.delete(id) }
626
+ @mutex.synchronize do
627
+ @own_sessions.delete(id)
628
+ @live.delete_if { |live| live.equal?(slot) }
629
+ end
567
630
  end
568
631
 
569
632
  def safe_close(label, &)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bidi2pdf
4
- VERSION = "0.1.23"
4
+ VERSION = "0.1.24"
5
5
  end
@@ -51,6 +51,12 @@ module Bidi2pdf
51
51
  # nor be killed before :close has been emitted.
52
52
  def close: (?untyped? error) -> untyped
53
53
 
54
+ # In a forked child: drops this process's copy of a connection the parent opened, without a word
55
+ # to the peer - no close frame, no TLS close_notify (the underlying TCP socket is closed, not the
56
+ # SSL one), no :close event - so the parent goes on using it. Closing a descriptor the parent also
57
+ # holds ends nothing for the peer; the reader thread did not survive the fork.
58
+ def release_after_fork: () -> untyped
59
+
54
60
  def open?: () -> untyped
55
61
 
56
62
  def closed?: () -> untyped
@@ -114,6 +114,9 @@ module Bidi2pdf
114
114
  def remove_event_listener: (*untyped names, untyped listener) -> untyped
115
115
 
116
116
  # Closes the WebSocket connection.
117
+ # In a forked child: lets go of the parent's connection locally (BufferedWebSocketClient#release_after_fork).
118
+ def release_after_fork: () -> untyped
119
+
117
120
  def close: () -> (nil | untyped)
118
121
 
119
122
  private
@@ -101,6 +101,10 @@ module Bidi2pdf
101
101
  # @return [String] The WebSocket URL.
102
102
  def websocket_url: () -> untyped
103
103
 
104
+ # In a forked child: drops this process's copies of the parent's connections - the command client
105
+ # and the event socket - without ending the session, which is the parent's (see #close for that).
106
+ def release_after_fork: () -> untyped
107
+
104
108
  private
105
109
 
106
110
  # Sends a command to the WebSocket client.
@@ -26,6 +26,8 @@ module Bidi2pdf
26
26
  # not stay registered (it would keep serving cold slots but never warm again).
27
27
  self.@instance: untyped
28
28
 
29
+ self.@instance_pid: untyped
30
+
29
31
  @config: untyped
30
32
 
31
33
  @slot_factory: untyped
@@ -34,6 +36,10 @@ module Bidi2pdf
34
36
 
35
37
  @available: untyped
36
38
 
39
+ # Every slot from its creation to its retirement - warm, out for a render, or in between - so a
40
+ # forked child can let go of all of them (#release_after_fork).
41
+ @live: untyped
42
+
37
43
  @replenish_threads: untyped
38
44
 
39
45
  @warming: untyped
@@ -180,9 +186,15 @@ module Bidi2pdf
180
186
 
181
187
  def self.safe_close: (untyped label) { () -> untyped } -> untyped
182
188
 
183
- # Returns the shared singleton warmer instance, creating it (and pre-warming it) on first call.
189
+ # Returns the shared singleton warmer instance, creating it (and pre-warming it) on first call -
190
+ # and in a forked child, its own one instead of the copy it inherited (see
191
+ # #abandon_inherited_instance).
184
192
  def self.instance: () -> untyped
185
193
 
194
+ # Warms this process's own slots now - for a forked child (a Puma worker's on_worker_boot), so
195
+ # its first render does not pay for them. Without it the child warms on its first render.
196
+ def self.after_fork: () -> untyped
197
+
186
198
  # Checks out a slot, yields a fresh tab for one render, then retires the slot.
187
199
  def self.with_tab: () { (?) -> untyped } -> untyped
188
200
 
@@ -190,11 +202,22 @@ module Bidi2pdf
190
202
  # warmer yet - this never creates one).
191
203
  def self.sweep!: (?reason: ::Symbol) -> untyped
192
204
 
193
- # Retires every currently-warm spare and resets the singleton.
205
+ # Retires every currently-warm spare and resets the singleton. In a forked child it only drops
206
+ # the inherited copy: those spares are the parent's.
194
207
  def self.shutdown: () -> untyped
195
208
 
196
209
  private
197
210
 
211
+ # The instance belongs to the process that created it. A forked child (a Puma worker - Rails
212
+ # boots the app, and with it this warmer, in the master before the fork) inherits a copy whose
213
+ # threads did not survive the fork and whose slots are its parent's, WebSocket connections
214
+ # included: a render on one sent its commands down the parent's socket, the parent's reader
215
+ # thread took the answers ("Unknown response"), and the child timed out. So the child lets go
216
+ # of its copy - closing only its own copies of the spare slots' sockets, never their sessions,
217
+ # which are its parent's (#release_after_fork) - and builds its own from the same
218
+ # configuration.
219
+ def self.abandon_inherited_instance: () -> (nil | untyped)
220
+
198
221
  # Records each part in +parts+ the moment it exists, so #default_slot_factory's rescue can
199
222
  # retire exactly what was created so far. Mirrors Launcher#session's local/remote branch.
200
223
  def self.build_slot: (untyped config, untyped parts) -> { session: untyped, browser: untyped, manager: untyped }
@@ -207,6 +230,19 @@ module Bidi2pdf
207
230
 
208
231
  def initialize: (untyped config, ?slot_factory: untyped?) -> void
209
232
 
233
+ # In a forked child, for the copy it inherited: closes the child's copies of the sockets of every
234
+ # slot the parent had - warm, out for a render, or between the two (being checked out, being
235
+ # retired) - locally (Session#release_after_fork: no close frame, no session end), and forgets the
236
+ # slots. The parent's sessions, its renders in flight, its leases and its threads are untouched.
237
+ # One slot that fails to release does not stop the others. The warmer itself is unusable
238
+ # afterwards - .instance builds the child its own.
239
+ #
240
+ # Not covered: a session still being built when the parent forked - between chromedriver's
241
+ # answer and #create_slot listing it, a fraction of a second. Its socket copy in the child is left
242
+ # to garbage collection. That needs a fork from a process that is building a slot on another
243
+ # thread at that moment; Puma's master never renders, so its forks do not meet it.
244
+ def release_after_fork: () -> untyped
245
+
210
246
  # Sweeps the remote chromedriver now - for an application that suspects leaked sessions.
211
247
  #
212
248
  # @return [ChromeSweeper::Result, nil] nil when no sweeper is configured.
@@ -1,3 +1,3 @@
1
1
  module Bidi2pdf
2
- VERSION: "0.1.23"
2
+ VERSION: "0.1.24"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bidi2pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.23
4
+ version: 0.1.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dieter S.