X-Recipient: archive-cygwin AT delorie DOT com X-SWARE-Spam-Status: No, hits=-0.7 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,SPF_HELO_PASS,TW_CG,T_RP_MATCHES_RCVD,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org To: cygwin AT cygwin DOT com From: Jason Curl Subject: Re: Bug in fork() while in a thread Date: Sun, 15 Aug 2010 22:56:07 +0200 Lines: 36 Message-ID: References: <20100815185353 DOT GA26993 AT ednor DOT casa DOT cgf DOT cx> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.11) Gecko/20100711 Thunderbird/3.0.6 In-Reply-To: <20100815185353.GA26993@ednor.casa.cgf.cx> X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com On 15/08/2010 20:53, Christopher Faylor wrote: > On Sun, Aug 15, 2010 at 07:42:01PM +0200, Jason Curl wrote: >> In particular, if I create a thread, then issue a fork(), data that >> exists on the stack is corrupted after the fork() is in the child. Using >> data on the heap doesn't show any issues (and is currently my >> workaround, in case this is a bug). > > If I'm reading this correctly then "the stack" in this case is the stack > associated with the main thread. Cygwin only duplicates the stack in > the executing thread. In your example, env (or presumably env2) from > the main thread is passed to another thread which then calls fork. In > that scenario, the forked process is going to see garbage in env since > the array has never been initialized. > > It is theoretically possible to duplicate the stack of the main thread > and other threads in the forked process but this isn't something that I, > personally, would want to take on. I'm the guy who wrote the code that > duplicates the state of the stack when a thread forks. It was done at > the request of a customer and it was very tricky to get right. This > isn't an experience I'd willingly take on again since it would be a lot > of work, would require testing on every Windows OS from NT4 on, and > would potentially slow down an already slow down Cygwin's already slow > fork() implementation. > > However, if this is something that you're willing to do, I'll happily > review a patch to the cygwin1.dll. You'd need to modify fork related > code in dcrt0.cc. Thanks Chris for the detailed reply. I've got a workaround and you've confirmed it is correct with your explanation. I don't get much time, my C++ needs improving, let's see what I can do. > > cgf > -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple