|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative '../support/console_test_case' |
| 4 | + |
| 5 | +module DEBUGGER__ |
| 6 | + class ForkBpSyncTest < ConsoleTestCase |
| 7 | + |
| 8 | + # Breakpoint set in parent AFTER fork is synced to child. |
| 9 | + # The child uses binding.break as a sync checkpoint -- when it enters |
| 10 | + # subsession there, bp_sync_check fires and picks up the new bp. |
| 11 | + def test_bp_set_after_fork_reaches_child |
| 12 | + code = <<~RUBY |
| 13 | + 1| pid = fork do |
| 14 | + 2| sleep 2 |
| 15 | + 3| binding.break # sync checkpoint |
| 16 | + 4| a = 1 # bp target (set from parent after fork) |
| 17 | + 5| end |
| 18 | + 6| sleep 0.1 |
| 19 | + 7| binding.break # parent stops here after fork |
| 20 | + 8| Process.waitpid pid |
| 21 | + RUBY |
| 22 | + |
| 23 | + debug_code(code) do |
| 24 | + type 'c' |
| 25 | + assert_line_num 7 |
| 26 | + type 'b 4' # set bp AFTER fork |
| 27 | + type 'c' # parent continues, bp_sync_publish writes to file |
| 28 | + assert_line_num 3 # child at sync checkpoint |
| 29 | + type 'c' # child continues, hits synced bp |
| 30 | + assert_line_num 4 |
| 31 | + type 'c' |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + # Breakpoint deleted in parent after fork is removed from child. |
| 36 | + # Child inherits bp via COW, but parent deletes it and publishes. |
| 37 | + # When child syncs at its binding.break, the bp is reconciled away. |
| 38 | + def test_bp_deleted_after_fork_removed_from_child |
| 39 | + code = <<~RUBY |
| 40 | + 1| pid = fork do |
| 41 | + 2| sleep 2 |
| 42 | + 3| binding.break # sync checkpoint |
| 43 | + 4| a = 1 # bp was inherited, then deleted via sync |
| 44 | + 5| binding.break # child should stop here instead |
| 45 | + 6| end |
| 46 | + 7| sleep 0.1 |
| 47 | + 8| binding.break # parent stops here |
| 48 | + 9| Process.waitpid pid |
| 49 | + RUBY |
| 50 | + |
| 51 | + debug_code(code) do |
| 52 | + type 'b 4' # set bp (will be inherited by child via COW) |
| 53 | + type 'c' |
| 54 | + assert_line_num 8 |
| 55 | + type 'del 0' # delete bp in parent |
| 56 | + type 'c' # parent continues, bp_sync_publish (empty set) |
| 57 | + assert_line_num 3 # child at sync checkpoint |
| 58 | + type 'c' # child continues, line 4 bp was removed by sync |
| 59 | + assert_line_num 5 # child skipped line 4 |
| 60 | + type 'c' |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + # Multiple children all receive synced breakpoints. |
| 65 | + # The flock serializes them: both stop at sync checkpoint first, then both hit the synced bp. |
| 66 | + def test_bp_sync_multiple_children |
| 67 | + code = <<~RUBY |
| 68 | + 1| pids = 2.times.map { |i| |
| 69 | + 2| fork do |
| 70 | + 3| sleep 2 |
| 71 | + 4| binding.break # sync checkpoint |
| 72 | + 5| a = i # bp target |
| 73 | + 6| end |
| 74 | + 7| } |
| 75 | + 8| sleep 0.5 |
| 76 | + 9| binding.break # parent stops |
| 77 | + 10| pids.each { |pid| Process.waitpid pid } |
| 78 | + RUBY |
| 79 | + |
| 80 | + debug_code(code) do |
| 81 | + type 'c' |
| 82 | + assert_line_num 9 |
| 83 | + type 'b 5' # set bp after both forks |
| 84 | + type 'c' # parent continues, publish |
| 85 | + assert_line_num 4 # child A at sync checkpoint |
| 86 | + type 'c' |
| 87 | + assert_line_num 4 # child B at sync checkpoint |
| 88 | + type 'c' |
| 89 | + assert_line_num 5 # synced bp hit |
| 90 | + type 'c' |
| 91 | + assert_line_num 5 # synced bp hit by other child |
| 92 | + type 'c' |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + # Catch breakpoint syncs to child process. |
| 97 | + def test_catch_bp_sync_after_fork |
| 98 | + code = <<~RUBY |
| 99 | + 1| pid = fork do |
| 100 | + 2| sleep 2 |
| 101 | + 3| binding.break # sync checkpoint |
| 102 | + 4| raise "test_error" |
| 103 | + 5| rescue |
| 104 | + 6| binding.break # child stops after rescue |
| 105 | + 7| end |
| 106 | + 8| sleep 0.5 |
| 107 | + 9| binding.break |
| 108 | + 10| Process.waitpid pid |
| 109 | + RUBY |
| 110 | + |
| 111 | + debug_code(code) do |
| 112 | + type 'c' |
| 113 | + assert_line_num 9 |
| 114 | + type 'catch RuntimeError' # set catch bp after fork |
| 115 | + type 'c' # parent continues, publish |
| 116 | + assert_line_num 3 # child at sync checkpoint |
| 117 | + type 'c' |
| 118 | + assert_line_num 4 # catch bp fires |
| 119 | + type 'c' |
| 120 | + assert_line_num 6 # child at rescue binding.break |
| 121 | + type 'c' |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + # Breakpoints set before fork work in child (inherited via COW + sync). |
| 126 | + # Regression test that sync doesn't break the existing behavior. |
| 127 | + def test_bp_before_fork_works_in_child |
| 128 | + code = <<~RUBY |
| 129 | + 1| pid = fork do |
| 130 | + 2| sleep 0.5 |
| 131 | + 3| a = 1 |
| 132 | + 4| end |
| 133 | + 5| Process.waitpid pid |
| 134 | + RUBY |
| 135 | + |
| 136 | + debug_code(code) do |
| 137 | + type 'b 3' |
| 138 | + type 'c' |
| 139 | + assert_line_num 3 |
| 140 | + type 'c' |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + # Stress test with multiple children and binding.break. |
| 145 | + # Regression test that fork_mode: :both behavior isn't broken by sync. |
| 146 | + def test_bp_sync_stress |
| 147 | + code = <<~RUBY |
| 148 | + 1| pids = 3.times.map do |
| 149 | + 2| fork do |
| 150 | + 3| sleep 1 |
| 151 | + 4| 2.times do |
| 152 | + 5| binding.break |
| 153 | + 6| end |
| 154 | + 7| end |
| 155 | + 8| end |
| 156 | + 9| sleep 0.1 |
| 157 | + 10| binding.break |
| 158 | + 11| pids.each { |pid| Process.waitpid pid rescue nil } |
| 159 | + RUBY |
| 160 | + |
| 161 | + debug_code(code) do |
| 162 | + type 'c' |
| 163 | + assert_line_num 10 |
| 164 | + type 'c' |
| 165 | + # 3 children x 2 iterations = 6 stops at line 5 |
| 166 | + 6.times do |
| 167 | + assert_line_num 5 |
| 168 | + type 'c' |
| 169 | + end |
| 170 | + end |
| 171 | + end |
| 172 | + end |
| 173 | +end |
0 commit comments