rails-profiler 0.44.12 → 0.44.14

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: 31af1f16fda6ca926b1dd3d4e13992e235b3006b8c81a238ce06d2e003a7793c
4
- data.tar.gz: 2ac428bf14f9c80cd5bd12729bacb4989ca0d29607bf6a98611877bcfe0924a9
3
+ metadata.gz: f304a45c85e9192bf75e9310c5226587d922efcde7c5b0e8556bc9806b44814c
4
+ data.tar.gz: bd8ae9a3a92bbea50328f57f17fd7ca2b7307929230ed70a1913d66c268a508e
5
5
  SHA512:
6
- metadata.gz: 80c65e06fb80613fc6f688dff701c16a91ed43428851c9217d6b986e0d0ea0836bd40ec10a0b72efe7086db6778bcbd303789cd4e75d61c83184e3eb2b767dc2
7
- data.tar.gz: 7ccd51df55f9698ee0264d9fe2dac70be0f365e07d7db37181f3ba545b25ca7c4029fad969b0b43456551b8b4e13e641331fe4a343f186dd768962e0b5253307
6
+ metadata.gz: bc552f921859972a5160aa7d8615e6ce9f725e71e2ef22e59d4ac804f598e77cfb69f908b0f8c9f86219c5d6e5bc2095b9ad4c3fc4e39f247c4a777c21a847cf
7
+ data.tar.gz: 131fea7f68ec9cb7abe63b6d9f426752bdcc22676ca8e7315bed758dc9097a007ebbfa0fc6e42f8b62a70ae21a98037be514bd9a30fd4ccf9cb7afa432fcbaca
data/CHANGELOG.md CHANGED
@@ -19,6 +19,22 @@ every commit of every tag interval is accounted for one way or the other.
19
19
 
20
20
  ## [Unreleased]
21
21
 
22
+ ## [0.44.14] - 2026-10-23
23
+
24
+ <!-- stamped -->
25
+
26
+ ### Fixed
27
+
28
+ - **SqliteStore:** discard the memoized storage instance after a fork so a fresh connection is built lazily on the next access, instead of reusing the parent's connection, which sqlite3 closes in the child (`preload_app!`, prefork scenarios).
29
+
30
+ ## [0.44.13] - 2026-10-23
31
+
32
+ <!-- stamped -->
33
+
34
+ ### Fixed
35
+
36
+ - **Compare:** a collector absent from the second profile being compared is now neutral too, like one absent from the first, instead of being classified as an improvement or a regression.
37
+
22
38
  ## [0.44.12] - 2026-10-23
23
39
 
24
40
  <!-- stamped -->
@@ -425,6 +425,13 @@ module Profiler
425
425
  @storage_backend || BACKEND_LOCK.synchronize { @storage_backend ||= build_storage_backend }
426
426
  end
427
427
 
428
+ # Called by Profiler.clear_storage_after_fork: @storage_backend is memoized here too, so
429
+ # clearing only Profiler's own @storage would hand the child back the very same (discarded)
430
+ # instance on its next Profiler.storage call.
431
+ def reset_storage_backend!
432
+ BACKEND_LOCK.synchronize { @storage_backend = nil }
433
+ end
434
+
428
435
  private
429
436
 
430
437
  def default_tmp_path
@@ -97,16 +97,16 @@ module Profiler
97
97
  delta = (b || 0) - (a || 0)
98
98
  delta_pct = (a && a != 0) ? (delta / a * 100).round(1) : nil
99
99
 
100
- status = compute_status(delta, delta_pct, direction, threshold_pct, val_a: val_a)
100
+ status = compute_status(delta, delta_pct, direction, threshold_pct, val_a: val_a, val_b: val_b)
101
101
 
102
102
  { value_a: val_a, value_b: val_b, delta: delta.round(4), delta_pct: delta_pct, status: status }
103
103
  end
104
104
 
105
- def compute_status(delta, delta_pct, direction, threshold_pct, val_a: nil)
105
+ def compute_status(delta, delta_pct, direction, threshold_pct, val_a: nil, val_b: nil)
106
106
  if delta == 0
107
107
  :neutral
108
- elsif val_a.nil?
109
- # Collector absent in baseline profile: no measurement to compare, stay neutral
108
+ elsif val_a.nil? || val_b.nil?
109
+ # Collector absent in one of the two profiles: no measurement to compare, stay neutral
110
110
  :neutral
111
111
  elsif delta_pct.nil?
112
112
  # Collector present but baseline is zero: apply direction logic directly without threshold
@@ -38,6 +38,32 @@ module Profiler
38
38
  migrate!
39
39
  end
40
40
 
41
+ # Called by Profiler.clear_storage_after_fork, on the parent's store, before the child
42
+ # drops its reference. sqlite3 >= 2.0 would otherwise print a warning (or, with WAL and
43
+ # sqlite3 < 2.0, let its finalizer close a handle the parent still owns) when this object
44
+ # is garbage-collected in the child. #discard only exists on sqlite3 >= 2.0. On an older
45
+ # gem the handle is kept alive instead, deferring the eventual close to process exit
46
+ # rather than an arbitrary GC pass while the worker is still using the database: Ruby still
47
+ # runs the finalizer on a live object at exit, so this delays the close, it does not avoid
48
+ # it. sqlite3 >= 2.0 also installs its own Process._fork hook (SQLite3::ForkSafety) that
49
+ # discards inherited connections on its own; depending on prepend order (whether sqlite3 or
50
+ # the profiler is required first) its warning may still appear once even with this method
51
+ # in place.
52
+ def discard_after_fork
53
+ if @db.respond_to?(:discard)
54
+ # Already discarded by sqlite3's own Process._fork hook (SQLite3::ForkSafety) when the
55
+ # sqlite3 gem was required before the profiler: discarding twice is at best a spurious
56
+ # error here, at worst a NULL sqlite3* reaching the C layer.
57
+ @db.discard unless @db.closed?
58
+ else
59
+ (self.class.kept_alive_after_fork ||= []) << @db
60
+ end
61
+ end
62
+
63
+ class << self
64
+ attr_accessor :kept_alive_after_fork
65
+ end
66
+
41
67
  def do_save(token, profile)
42
68
  data = profile.to_h
43
69
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Profiler
4
- VERSION = "0.44.12"
4
+ VERSION = "0.44.14"
5
5
 
6
6
  GEM_ROOT = File.expand_path("../..", __dir__)
7
7
 
data/lib/profiler.rb CHANGED
@@ -14,6 +14,16 @@ module Profiler
14
14
  # was parsing.
15
15
  LOG_ERROR_MESSAGE_LIMIT = 1000
16
16
 
17
+ # Detects process fork (Ruby 3.1+) and clears the storage instance in the child,
18
+ # so it is recreated with a fresh database connection.
19
+ module ForkHook
20
+ def _fork
21
+ pid = super
22
+ Profiler.clear_storage_after_fork if pid.zero?
23
+ pid
24
+ end
25
+ end
26
+
17
27
  class << self
18
28
  attr_writer :configuration
19
29
  attr_accessor :function_profiling_enabled
@@ -40,11 +50,32 @@ module Profiler
40
50
  # A store that cannot be created is said once (Storage::Unavailable) and tried again on the
41
51
  # next call.
42
52
  def storage
53
+ install_fork_hook!
43
54
  @storage || STORAGE_LOCK.synchronize { @storage ||= configuration.storage_backend }
44
55
  rescue StandardError => e
45
56
  Storage::Unavailable.for(e)
46
57
  end
47
58
 
59
+ private
60
+
61
+ def install_fork_hook!
62
+ return if @fork_hook_installed || !Process.respond_to?(:_fork)
63
+
64
+ Process.singleton_class.prepend(ForkHook)
65
+ @fork_hook_installed = true
66
+ end
67
+
68
+ public
69
+
70
+ def clear_storage_after_fork
71
+ old_storage = @storage
72
+ @storage = nil
73
+ configuration.reset_storage_backend!
74
+ old_storage.discard_after_fork if old_storage.respond_to?(:discard_after_fork)
75
+ rescue => e
76
+ log_warn("could not clear storage after fork", e)
77
+ end
78
+
48
79
  # Saves a profile from a path of the application (a job, a console command, a test, an
49
80
  # outbound HTTP call): a storage error loses the profile, never the application's work.
50
81
  # True when saved; false when the error was logged instead, or when the store is unavailable
@@ -251,6 +282,9 @@ module Profiler
251
282
  self.function_profiling_mode = "lite"
252
283
  self.function_profiling_clock = "wall"
253
284
  self.function_profiling_tracepoint_fallback = false
285
+
286
+ # Install fork hook as early as possible, so it's in place before any fork happens
287
+ install_fork_hook! if Process.respond_to?(:_fork)
254
288
  end
255
289
 
256
290
  # Require core components
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.44.12
4
+ version: 0.44.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sébastien Duplessy