Bug #6131

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

Added by Luis Lavena about 2 years ago. Updated about 2 years ago.

[ruby-core:43245]
Status:Closed
Priority:Normal
Assignee:Hiroshi Shirosaki
Category:core
Target version:1.9.3
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 about 2 years ago

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

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

Revision 35250
Added by shirosaki about 2 years ago

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

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

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

  • process.c (rbexecarg_addopt): ditto.

  • process.c (rbspawnprocess): ditto.

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

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

  • test/ruby/testthread.rb
    (TestThreadGroup#test
    threadtimerand_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 (rbw32spawn): ditto.

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

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

History

#1 Updated by Shyouhei Urabe about 2 years ago

  • Status changed from Open to Assigned

#2 Updated by Heesob Park about 2 years ago

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

If the CREATENEWPROCESS_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 2 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 2 years ago

How about removing CREATENEWPROCESSGROUP flag by default and providing a way to specify CREATENEWPROCESSGROUP flag by spawn methods's arguments?
Ruby user can specify CREATENEWPROCESS_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::CREATENEWPROCESS_GROUP }

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

#5 Updated by Nobuyoshi Nakada about 2 years ago

h.shirosaki (Hiroshi Shirosaki) wrote:

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

Silly option. Should be 'newprocessgroup: 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 about 2 years ago

nobu (Nobuyoshi Nakada) wrote:

Should be 'newprocessgroup: 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 CREATENEWPROCESSGROUP flag by default
- add option new_pgroup: true to specify CREATE
NEWPROCESSGROUP flag

Any opinions?

#7 Updated by Hiroshi Shirosaki about 2 years ago

  • Assignee changed from Hiroshi Shirosaki to Usaku NAKAMURA

Assigned to usa-san for comments.

#8 Updated by Usaku NAKAMURA about 2 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 about 2 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 CREATENEWPROCESSGROUP, so can be backported to ruby193 branch
  • Implement new_pgroup option for spawn.

Thank you.

#10 Updated by Anonymous about 2 years ago

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

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/testthread.rb
    (TestThreadGroup#test
    threadtimerandinterrupt): skip on Windows.
    Process.kill cannot kill a subprocess if CREATE
    NEWPROCESSGROUP
    flag is not specified in a call to CreateProcessW().

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

#11 Updated by Hiroshi Shirosaki about 2 years ago

Hi Luis,

Hiroshi: will be good do this in two commits:

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

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

Also available in: Atom PDF