Bug #6131

Ctrl-C handler do not work from exec process (Windows)

Added by Luis Lavena about 3 years ago. Updated almost 3 years ago.

[ruby-core:43245]
Status:Closed
Priority:Normal
Assignee:Hiroshi Shirosaki
ruby -v:ruby 1.9.3-p155 (2012-03-03) [i386-mingw32] Backport:

Description

Hello,

Originally reported to TheCodeShop group: https://groups.google.com/forum/?fromgroups#!topic/thecodeshop/ZI9FY-RKYgs

The usage of "rails server" uses exec to spawn another process to run a server, and that process do not handle Ctrl-C.

Script to recreate this issue:

https://gist.github.com/2009356

=begin a.rb
trap(:INT) do
puts "Hit Ctrl+C in a.rb"
end

exec "ruby b.rb"
=end

=begin b.rb
trap(:INT) do
puts "Hit Ctrl+C in b.rb"
exit(false)
end

sleep
=end

Invoking b directly from command line works, but from a.rb it fails to respond to Ctrl-C

process.patch Magnifier (9.09 KB) Hiroshi Shirosaki, 04/03/2012 10:44 PM


Related issues

Related to Ruby trunk - Feature #1952: cannot stop with Ctrl+C Closed 08/18/2009

Associated revisions

Revision 35249
Added by shirosaki almost 3 years ago

  • test/ruby/test_thread.rb
    (TestThreadGroup#test_thread_timer_and_interrupt): skip on Windows.
    Process.kill cannot kill a subprocess if CREATE_NEW_PROCESS_GROUP
    flag is not specified in a call to CreateProcessW().

  • win32/win32.c (CreateChild): revert the usage of
    CREATE_NEW_PROCESS_GROUP flag for compatibility.
    [Bug #6131]

Revision 35249
Added by shirosaki almost 3 years ago

  • test/ruby/test_thread.rb
    (TestThreadGroup#test_thread_timer_and_interrupt): skip on Windows.
    Process.kill cannot kill a subprocess if CREATE_NEW_PROCESS_GROUP
    flag is not specified in a call to CreateProcessW().

  • win32/win32.c (CreateChild): revert the usage of
    CREATE_NEW_PROCESS_GROUP flag for compatibility.
    [Bug #6131]

Revision 35250
Added by shirosaki almost 3 years ago

  • include/ruby/win32.h (rb_w32_aspawn_flags): add the declaration of
    new function.

  • process.c (enum): add EXEC_OPTION_PGROUP and move the position
    above for the usage in proc_spawn_n().

  • process.c (proc_spawn_n): add an argument to pass new option
    new_pgroup. The option specifies CREATE_NEW_PROCESS_GROUP flag to
    CreateProcessW(). This flag is necessary for the usage of
    Process.kill on the subprocess on Windows.

  • process.c (rb_exec_arg_addopt): ditto.

  • process.c (rb_spawn_process): ditto.

  • process.c (documentation for rb_f_spawn): add documentation for new
    option new_pgroup of spawn.

  • test/ruby/test_process.rb (TestProcess#test_execopts_new_pgroup):
    add tests for option new_pgroup.

  • test/ruby/test_thread.rb
    (TestThreadGroup#test_thread_timer_and_interrupt):
    add option new_pgroup: true to spawn on Windows. It's needed for
    Process.kill on a subprocess.

  • win32/win32.c (CreateChild): add an argument to pass
    dwCreationFlags of CreateProcessW().

  • win32/win32.c (rb_w32_spawn): ditto.

  • win32/win32.c (rb_w32_aspawn_flags): add new function to pass
    dwCreationFlags.

  • win32/win32.c (rb_w32_aspawn): refactor to move the content to
    rb_w32_aspawn_flags().
    [Bug #6131]

Revision 35250
Added by shirosaki almost 3 years ago

  • include/ruby/win32.h (rb_w32_aspawn_flags): add the declaration of
    new function.

  • process.c (enum): add EXEC_OPTION_PGROUP and move the position
    above for the usage in proc_spawn_n().

  • process.c (proc_spawn_n): add an argument to pass new option
    new_pgroup. The option specifies CREATE_NEW_PROCESS_GROUP flag to
    CreateProcessW(). This flag is necessary for the usage of
    Process.kill on the subprocess on Windows.

  • process.c (rb_exec_arg_addopt): ditto.

  • process.c (rb_spawn_process): ditto.

  • process.c (documentation for rb_f_spawn): add documentation for new
    option new_pgroup of spawn.

  • test/ruby/test_process.rb (TestProcess#test_execopts_new_pgroup):
    add tests for option new_pgroup.

  • test/ruby/test_thread.rb
    (TestThreadGroup#test_thread_timer_and_interrupt):
    add option new_pgroup: true to spawn on Windows. It's needed for
    Process.kill on a subprocess.

  • win32/win32.c (CreateChild): add an argument to pass
    dwCreationFlags of CreateProcessW().

  • win32/win32.c (rb_w32_spawn): ditto.

  • win32/win32.c (rb_w32_aspawn_flags): add new function to pass
    dwCreationFlags.

  • win32/win32.c (rb_w32_aspawn): refactor to move the content to
    rb_w32_aspawn_flags().
    [Bug #6131]

History

#1 Updated by Shyouhei Urabe about 3 years ago

  • Status changed from Open to Assigned

#2 Updated by Heesob Park about 3 years ago

As you may know, this bug is due to the Revision r34387.

If the CREATE_NEW_PROCESS_GROUP flag is specified in a call to the CreateProcess function, CTRL+C signal is disabled for all processes within the new process group.

I think r34387 is better to be reverted for compatibility with existing applications.

#3 Updated by Usaku NAKAMURA about 3 years ago

  • Assignee changed from Usaku NAKAMURA to Hiroshi Shirosaki

r34389 (not 34387) is the revision.

Shirosaki-san, how do you think about this?

#4 Updated by Hiroshi Shirosaki about 3 years ago

How about removing CREATE_NEW_PROCESS_GROUP flag by default and providing a way to specify CREATE_NEW_PROCESS_GROUP flag by spawn methods's arguments?
Ruby user can specify CREATE_NEW_PROCESS_GROUP flag if it's needed.

spawn method has options parameter hash.

spawn(command, options={}) -> Integer
spawn(env, command, options={}) -> Integer
spawn(env, program, *args, options={}) -> Integer

Specify options:
{ :creationflags => Process::CREATE_NEW_PROCESS_GROUP }

Python module does similar things.
http://docs.python.org/library/subprocess.html#subprocess.CREATE_NEW_PROCESS_GROUP

#5 Updated by Nobuyoshi Nakada about 3 years ago

h.shirosaki (Hiroshi Shirosaki) wrote:

Specify options:
{ :creationflags => Process::CREATE_NEW_PROCESS_GROUP }

Silly option. Should be 'new_process_group: true' or similar, at least.

Python module does similar things.
http://docs.python.org/library/subprocess.html#subprocess.CREATE_NEW_PROCESS_GROUP

Sigh.

#6 Updated by Hiroshi Shirosaki almost 3 years ago

nobu (Nobuyoshi Nakada) wrote:

Should be 'new_process_group: true' or similar, at least.

How about new_pgroup: true? :pgroup option already exists and this is alike pgroup.

I created a patch attached.
- remove CREATE_NEW_PROCESS_GROUP flag by default
- add option new_pgroup: true to specify CREATE_NEW_PROCESS_GROUP flag

Any opinions?

#7 Updated by Hiroshi Shirosaki almost 3 years ago

  • Assignee changed from Hiroshi Shirosaki to Usaku NAKAMURA

Assigned to usa-san for comments.

#8 Updated by Usaku NAKAMURA almost 3 years ago

Hello,

In message " [ruby-trunk - Bug #6131] Ctrl-C handler do not work from exec process (Windows)"
on Apr.06,2012 18:55:27, h.shirosaki@gmail.com wrote:

Assigned to usa-san for comments.

Oops, sorry, I overlooked.

I also considered the alternative plan, but I got no idea.
Let it go. Shirosaki-san, please commit it.

Regards,
--
U.Nakamura usa@garbagecollect.jp

#9 Updated by Luis Lavena almost 3 years ago

  • Assignee changed from Usaku NAKAMURA to Hiroshi Shirosaki

Hiroshi: will be good do this in two commits:

  • First commit that reverts the usage of CREATE_NEW_PROCESS_GROUP, so can be backported to ruby_1_9_3 branch
  • Implement new_pgroup option for spawn.

Thank you.

#10 Updated by Anonymous almost 3 years ago

  • % Done changed from 0 to 100
  • Status changed from Assigned to Closed

This issue was solved with changeset r35249.
Luis, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • test/ruby/test_thread.rb
    (TestThreadGroup#test_thread_timer_and_interrupt): skip on Windows.
    Process.kill cannot kill a subprocess if CREATE_NEW_PROCESS_GROUP
    flag is not specified in a call to CreateProcessW().

  • win32/win32.c (CreateChild): revert the usage of
    CREATE_NEW_PROCESS_GROUP flag for compatibility.
    [Bug #6131]

#11 Updated by Hiroshi Shirosaki almost 3 years ago

Hi Luis,

Hiroshi: will be good do this in two commits:

  • First commit that reverts the usage of CREATE_NEW_PROCESS_GROUP, so can be backported to ruby_1_9_3 branch
  • Implement new_pgroup option for spawn.

I've committed this in two commits. Please check.

Also available in: Atom PDF