bugGNUstep - Bugs: bug #44240, NSThread leaks a run loop if...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #44240: NSThread leaks a run loop if NSFileHandle -finalize is called during deallocation

Submitted by:  Wolfgang Lux <wlux>
Submitted on:  Fri 13 Feb 2015 05:58:17 PM UTC  
 
Category: NoneSeverity: 3 - Normal
Item Group: NoneStatus: Fixed
Privacy: PublicAssigned to: None
Open/Closed: Closed

Tue 17 Feb 2015 05:41:38 PM UTC, comment #2:

That change looks reasonable to me and fixes the problem.
However, I think that there's also an issue with the -ignoreReadDescriptor and -ignoreWriteDescriptor methods from NSFileHandle. It looks fairly stupid to me to create a new run loop for the current thread when are just about to delete the thread and its run loop. But what looks worse to me is that this code will remove the file descriptor from the run loop of the current thread only. It doesn't handle the case where the file descriptor is added to the run loop of a secondary thread, but then closed on the main thread (or vice versa). My feeling is that if we call -removeEvent:type:forMode:all: we should do that for all (and only those) run loops that currently exist.

Wolfgang Lux <wlux>
Sat 14 Feb 2015 05:56:37 AM UTC, comment #1:

Thanks ... I fixed in trunk by looking round and releasing the thread run loop and variables repeatedly (up to ten times) to catch cases where the deallocation of an object on thread exit actually repopulates either.
Of course that's not completely foolproof, but if the code finds that the thread ivars keep on being repopulated, it NSLogs() that fact before giving up.

Thanks for the test program, which made it very easy to check behavior before/after the change.

Richard Frith-Macdonald <CaS>
Project Member
Fri 13 Feb 2015 05:58:17 PM UTC, original submission:

I've come across a bug where my program was leaking file descriptors. It turns out that this bug was happening if NSFileHandle -finalize is called while the NSThread -dealloc method is called, e.g., because a NSFileHandle object is released while the target or argument attributes of the NSThread instance are deallocated.
This happens because the thread's run loop is deallocated early on in the -dealloc method, in particular before the _target and _arg attributes are released. The NSFileHandle -finalize method, on the other hand, calls -ignoreReadDescriptor and -ignoreWriteDescriptor and both methods ask for the current thread's run loop. Thus, if -finalize is called from within NSThread -dealloc, a new run loop is created and saved in the thread object, but then never gets released.
You can see this with the attached test program. Compile the attached program with the commands
gcc `gnustep-config --objc-flags` -c Tester.m
gcc `gnustep-config --objc-flags` -o Tester Tester.o `gnustep-config --base-libs`
(use clang instead of gcc if you wish so). Then run ./Tester from the command line. The program displays a prompt where you can enter a file name. For each name that you enter, the program reads the named file in a background thread (starting after a delay of 1 second) and once finished it prints the size of the file and the memory statistics. Watch how a GSRunLoopThreadInfo and NSRunLoop leak on each iteration after you have entered the third filename.

Wolfgang Lux <wlux>

 

Attached Files
file #33064:  Tester.m added by wlux (3KiB - application/octet-stream)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by CaS (Posted a comment)
  • -unavailable- added by wlux (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 3 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Sat 14 Feb 2015 05:56:37 AM UTCCaSStatusNone=>Fixed
      Open/ClosedOpen=>Closed
    Fri 13 Feb 2015 05:58:17 PM UTCwluxAttached File-=>Added Tester.m, #33064

    Back to the top


    Powered by Savane 3.1-cleanup1