Skip to content

Commit 72126af

Browse files
authored
Merge pull request #191 from SciRuby/poll_parent_process
Add parent process poller for UNIX
2 parents 1bf666b + cabb4b4 commit 72126af

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

lib/iruby/kernel.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def initialize(config_file)
1919
$stdout = OStream.new(@session, :stdout)
2020
$stderr = OStream.new(@session, :stderr)
2121

22+
init_parent_process_poller
23+
2224
@execution_count = 0
2325
@backend = create_backend
2426
@running = true
@@ -168,5 +170,37 @@ def comm_close(msg)
168170
Comm.comm[comm_id].handle_close(msg[:content]['data'])
169171
Comm.comm.delete(comm_id)
170172
end
173+
174+
private
175+
176+
def init_parent_process_poller
177+
pid = ENV.fetch('JPY_PARENT_PID', 0).to_i
178+
return unless pid > 1
179+
180+
case RUBY_PLATFORM
181+
when /mswin/, /mingw/
182+
# TODO
183+
else
184+
@parent_poller = start_parent_process_pollar_unix
185+
end
186+
end
187+
188+
def start_parent_process_pollar_unix
189+
Thread.start do
190+
IRuby.logger.warn("parent process poller thread started.")
191+
loop do
192+
begin
193+
current_ppid = Process.ppid
194+
if current_ppid == 1
195+
IRuby.logger.warn("parent process appears to exited, shutting down.")
196+
exit!(1)
197+
end
198+
sleep 1
199+
rescue Errno::EINTR
200+
# ignored
201+
end
202+
end
203+
end
204+
end
171205
end
172206
end

0 commit comments

Comments
 (0)