Bug 40632 - spawned process through exec task blocks parent exec task
Summary: spawned process through exec task blocks parent exec task
Status: NEW
Alias: None
Product: Ant
Classification: Unclassified
Component: Core tasks (show other bugs)
Version: 1.7.0
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---
Assignee: Ant Notifications List
Depends on:
Reported: 2006-09-28 17:23 UTC by Georges Zwingelstein
Modified: 2011-07-25 02:24 UTC (History)
2 users (show)

Ant script demonstrating the environment, the problem and the (18.58 KB, application/zip)
2006-09-28 17:25 UTC, Georges Zwingelstein
Runs arguments with bIneheritHandle to FALSE (16.69 KB, application/x-zip-compressed)
2006-10-18 10:04 UTC, Georges Zwingelstein

Note You need to log in before you can comment on or make changes to this bug.
Description Georges Zwingelstein 2006-09-28 17:23:06 UTC
Under Windows, a process inherits handles even when spawned using the Exec task
with the spawn attribute. This makes an Exec task that calls another Exec task
with the spawn attribute to block until the spawned process exits.

The source of the problem may be in the JVM where a spawned process may be
created using the Win32 API CreateProcess with the bInheritHandles attribute set
to TRUE, while it should be FALSE for spawned processes.

A workaround can be to insert a process that will spawn the original process
with the bInheritHandles attribute set to FALSE.

See attached Ant script demonstrating the environment, the problem and the
Comment 1 Georges Zwingelstein 2006-09-28 17:25:11 UTC
Created attachment 18934 [details]
Ant script demonstrating the environment, the problem and the
Comment 2 Peter Reilly 2006-09-28 17:30:27 UTC
Thanks for the report.
I think that this is the same probles
except that you set the spawn attribute and still
see the problem
Comment 3 Matt Benson 2006-09-28 17:42:45 UTC
But what could we do in Ant to get around the original problem?
Comment 4 Antoine Levy-Lambert 2006-09-29 08:03:59 UTC
Hello Matt,

we could make a slightly modified version of this CreateDetachedProcess.cpp
which would just create a really detached process without inherited handles, and
ship Ant with CreateDetachedProcess.exe in the bin directory, and wrap spawning
under Win32 with this CreateDetachedProcess.exe.

The CreateDetachedProcess.cpp in the attached zip file has this line :

  LPTSTR lpCommandLine = "Notepad";

if instead we write the Microsoft VC++ equivalent of :

LPTSTR lpCommandLine = join(argv, " "); // do not know which idiom we could use here

then we would have something.

The other thing we can do is have a look whether Sun has done some work on this
issue in JDK 1.6. Else one of us could write in the BugParade, so that the issue
gets addressed.


Comment 5 Steve Loughran 2006-09-29 08:52:42 UTC
I'd rather push this towards Sun and Harmony. I dont want to get into the
problem of rewriting the java process API. Actually, I do, but not now; not in Ant.

There's lots of things in ::CreateProcess() that Java hides, just as there's
some options in ::LoadLibrary() that I'd have but which Sun dont expose in the
JNI library loading.
Comment 6 Matt Benson 2006-09-29 14:14:29 UTC
Yes, I thought we had had discussion in the past that steered towards the
decision of whether to include C (or Cpp or whatever) code in Ant, and usually
those were met with general reluctance.  ;)
Comment 7 Georges Zwingelstein 2006-09-29 15:42:57 UTC
I reproduced the problem with Java 1.6 Beta2 and I started the thread
http://forum.java.sun.com/thread.jspa?threadID=772669 titled "Spawned process
blocks grand parent" regarding this issue with a Java only example.
Comment 8 Georges Zwingelstein 2006-10-13 06:25:41 UTC
I submitted the problem reported in the thread
http://forum.java.sun.com/thread.jspa?threadID=772669 titled "Spawned process
blocks grand parent" as a bug on the Java Bug Database at http://bugs.sun.com. 
It has been accepted as a new bug and entered the bug into the internal bug 
tracking system under Bug Id: 6481278. You can vote for the bug at 
Comment 9 Andreas Krüger 2006-10-17 11:09:05 UTC
For the record: Peter Reilly seems to think that there is a connection between
this and bug 5003 and bug 37787.
Comment 10 Georges Zwingelstein 2006-10-18 10:04:57 UTC
Created attachment 19022 [details]
Runs arguments with bIneheritHandle to FALSE

Allows to test if setting the bInheritHandles to FALSE will fix problem related
to this. To use it, replace the executable in the exec task with this program
and insert the old executable as first argument.
<exec executable="notepad">
  <arg file="test.txt"/>
would become:
<exec executable="SpawnDetachedProcess">
  <arg value="notepad"/>
  <arg file="test.txt"/>
Comment 11 Peter Reilly 2006-10-20 04:38:52 UTC
You can use the forget task from ant-contrib to emulate this.
The forget task simply a <sequential> that
executes its tasks in a new thread, the daemon
attribute of which is true.

<ac:forget xmlns:ac="antlib:net.sf.antcontrib">
  <exec executable="notepad.exe" spawn="true"/>