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

Group
 
 

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

Submitter:  Wolfgang Lux <wlux>
Submitted:  Fri 13 Feb 2015 05:58:17 PM UTC
   
 
Category:  None Severity:  3 - Normal
Item Group:  None Status:  Fixed
Privacy:  Public Assigned to:  None
Open/Closed:  Closed
* Mandatory Fields

Add a New Comment Rich Markup
   

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>
Group 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>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

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
  • -email is unavailable- added by CaS (Posted a comment)
  • -email is unavailable- added by wlux (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2015-02-14 CaS StatusNone Fixed
        Open/ClosedOpen Closed
    2015-02-13 wlux Attached File- Added Tester.m, #33064

    Back to the top

    Powered by Savane 3.13-caa5.
    Corresponding source code