Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #159 (closed defect: fixed)

Opened 17 years ago

Last modified 17 years ago

Deadlock in string concatenation routine

Reported by: sean Assigned to: sean
Priority: major Milestone: 1.0
Component: Core Functionality Version:
Keywords: Cc:

Description

Nick wrote:

After some digging it turns out that the problem is actually in string concatination. So a simpler example is to exchange the call format(1); with char[] r; r ~= "a";

What's happening is this:

The compiler runtime relies on gc_free to finalize t, which in turn waits on the child thread. At the same time, the child thread attempts to allocate memory for the string concatenation and is forced to wait for the gc_free call to finish. This is a classic deadlock situation.

I think the best fix for this would be for the compiler runtime code (gc.d: _d_delclass) to explicitly call the object's finalizer and for gc_free to simply free memory. So the GC would only be responsible for finalizing objects whose lifetime ends during a collection, not for those destroyed as the result of a delete operation. The consequence of this would be that a call to gc_free for an arbitrary block of memory will not call the finalizer for that block, even if one is known to exist, but this seems a clear separation of responsibilities IMO. If the user really wants the finalizer called he can cast the pointer to Object and delete it.

A possible compromise would be for _d_delclass to explicitly finalize the object and then set a flag indicating that gc_free should not finalize the block, and for gc_free to finalize so long as the 'finalize' flag is still set. This may be a bit slower depending on how it's implemented, but it would allow gc_free to finalize tagged blocks when appropriate. But again, I think it is probably more appropriate for the GC to only finalize on collections, and assume that if gc_free is called at other times, then the user does not intend for finalization to occur.

Change History

12/28/06 20:15:57 changed by sean

Just a quick note. I've added this as a bug because it needs to be fixed, but I'm hoping that Walter will sort it out in Phobos and we'll just be able to merge in the changes. This is issue 649 in the D issue tracking system. Link here: http://d.puremagic.com/issues/show_bug.cgi?id=649

01/02/07 20:02:40 changed by sean

  • status changed from new to closed.
  • resolution set to fixed.

I decided to go ahead and fix this. Now, gc_free will not finalize memory, even if the FINALIZE bit is set. Instead, delete should be called if finalization is desired outside of a collection. The relevant changes are in commit [1167].