www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Forking problems on OS X 2.047

reply Byron Heads <wyverex.cypher gmail.com> writes:
When I use fork I am getting a core.thread.ThreadException: Unable to 
load thread state exception.

This is dmd 2.047 on OS X
I am trying to convert a small C application.


module fork;
import  core.sys.posix.unistd,
        std.stdio;

void main() 
{
    auto pid = fork();

    if( pid > 0 ) { 
        writeln( "Parent" );
    } else if( pid == 0 ) { 
        writeln( "Child" );
    } else {
        writeln( "Failed to fork!" );
    }   
}


$ ./fork
Parent
Child
core.thread.ThreadException: Unable to load thread state
----------------
5   fork                                0x00006de9 thread_suspendAll + 85
6   fork                                0x0000aff0 
D2gc3gcx3Gcx11fullcollectMFPvZk + 32
7   fork                                0x0000afbd 
D2gc3gcx3Gcx16fullcollectshellMFZk + 41
8   fork                                0x0000a0a2 
D2gc3gcx2GC18fullCollectNoStackMFZv + 54
9   fork                                0x0000803c gc_term + 44
10  fork                                0x0000cef6 
D2rt6dmain24mainUiPPaZi6runAllMFZv + 90
11  fork                                0x0000cdc6 
D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv + 42
12  fork                                0x0000cd54 main + 168
13  fork                                0x00001a85 start + 53

I tried different flags but the output is the same.
Jun 21 2010
next sibling parent Byron Heads <wyverex.cypher gmail.com> writes:
On Mon, 21 Jun 2010 21:32:21 +0000, Byron Heads wrote:

 When I use fork I am getting a core.thread.ThreadException: Unable to
 load thread state exception.
 
 This is dmd 2.047 on OS X
 I am trying to convert a small C application.
 
This runs fine under linux -Byron
Jun 21 2010
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2010-06-21 23:32, Byron Heads wrote:
 When I use fork I am getting a core.thread.ThreadException: Unable to
 load thread state exception.

 This is dmd 2.047 on OS X
 I am trying to convert a small C application.


 module fork;
 import  core.sys.posix.unistd,
          std.stdio;

 void main()
 {
      auto pid = fork();

      if( pid>  0 ) {
          writeln( "Parent" );
      } else if( pid == 0 ) {
          writeln( "Child" );
      } else {
          writeln( "Failed to fork!" );
      }
 }


 $ ./fork
 Parent
 Child
 core.thread.ThreadException: Unable to load thread state
 ----------------
 5   fork                                0x00006de9 thread_suspendAll + 85
 6   fork                                0x0000aff0
 D2gc3gcx3Gcx11fullcollectMFPvZk + 32
 7   fork                                0x0000afbd
 D2gc3gcx3Gcx16fullcollectshellMFZk + 41
 8   fork                                0x0000a0a2
 D2gc3gcx2GC18fullCollectNoStackMFZv + 54
 9   fork                                0x0000803c gc_term + 44
 10  fork                                0x0000cef6
 D2rt6dmain24mainUiPPaZi6runAllMFZv + 90
 11  fork                                0x0000cdc6
 D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv + 42
 12  fork                                0x0000cd54 main + 168
 13  fork                                0x00001a85 start + 53

 I tried different flags but the output is the same.
It's working fine using Tango on Snow Leopard but it doesn't work using D2 on Leopard or Snow. -- /Jacob Carlborg
Jun 22 2010
prev sibling next sibling parent "Masahiro Nakagawa" <repeatedly gmail.com> writes:
On Tue, 22 Jun 2010 06:32:21 +0900, Byron Heads <wyverex.cypher gmail.com>  
wrote:

 When I use fork I am getting a core.thread.ThreadException: Unable to
 load thread state exception.

 This is dmd 2.047 on OS X
 I am trying to convert a small C application.


 module fork;
 import  core.sys.posix.unistd,
         std.stdio;

 void main()
 {
     auto pid = fork();

     if( pid > 0 ) {
         writeln( "Parent" );
     } else if( pid == 0 ) {
         writeln( "Child" );
     } else {
         writeln( "Failed to fork!" );
     }
 }


 $ ./fork
 Parent
 Child
 core.thread.ThreadException: Unable to load thread state
 ----------------
 5   fork                                0x00006de9 thread_suspendAll + 85
 6   fork                                0x0000aff0
 D2gc3gcx3Gcx11fullcollectMFPvZk + 32
 7   fork                                0x0000afbd
 D2gc3gcx3Gcx16fullcollectshellMFZk + 41
 8   fork                                0x0000a0a2
 D2gc3gcx2GC18fullCollectNoStackMFZv + 54
 9   fork                                0x0000803c gc_term + 44
 10  fork                                0x0000cef6
 D2rt6dmain24mainUiPPaZi6runAllMFZv + 90
 11  fork                                0x0000cdc6
 D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv + 42
 12  fork                                0x0000cd54 main + 168
 13  fork                                0x00001a85 start + 53

 I tried different flags but the output is the same.
My environment too. Please put this bug into bugzilla. Masahiro
Jun 22 2010
prev sibling next sibling parent soul8o8 <soul8o8 gmail.com> writes:
Correction:
Of course, you shouldn't call thread_attachThis() for the parent thread. =
It's already been attached since the D runtime created it. (In fact, the =
documentation says: "If [thread_attachThis()] is called for a thread =
which is already registered, the result is undefined." My bad.)

BR
/soul


On Jun 21, 2010, at 23:32 , Byron Heads wrote:

 When I use fork I am getting a core.thread.ThreadException: Unable to=20=
 load thread state exception.
=20
 This is dmd 2.047 on OS X
 I am trying to convert a small C application.
=20
=20
 module fork;
 import  core.sys.posix.unistd,
        std.stdio;
=20
 void main()=20
 {
    auto pid =3D fork();
=20
    if( pid > 0 ) {=20
        writeln( "Parent" );
    } else if( pid =3D=3D 0 ) {=20
        writeln( "Child" );
    } else {
        writeln( "Failed to fork!" );
    }  =20
 }
=20
=20
 $ ./fork
 Parent
 Child
 core.thread.ThreadException: Unable to load thread state
 ----------------
 5   fork                                0x00006de9 thread_suspendAll + =
85
 6   fork                                0x0000aff0=20
 D2gc3gcx3Gcx11fullcollectMFPvZk + 32
 7   fork                                0x0000afbd=20
 D2gc3gcx3Gcx16fullcollectshellMFZk + 41
 8   fork                                0x0000a0a2=20
 D2gc3gcx2GC18fullCollectNoStackMFZv + 54
 9   fork                                0x0000803c gc_term + 44
 10  fork                                0x0000cef6=20
 D2rt6dmain24mainUiPPaZi6runAllMFZv + 90
 11  fork                                0x0000cdc6=20
 D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv + 42
 12  fork                                0x0000cd54 main + 168
 13  fork                                0x00001a85 start + 53
=20
 I tried different flags but the output is the same.
Jun 22 2010
prev sibling parent soul8o8 <soul8o8 gmail.com> writes:
I get the same error.

Fix:
The following code works allright on MacOS X 10.6.4 / DMD v2.047:


module fork;
import  core.sys.posix.unistd,
        core.thread, 		// added
        std.stdio;

void main()
{
    auto pid =3D fork();

    if( pid>  0 ) {
        thread_attachThis(); 	// added
        writeln( "Parent" );
    } else if( pid =3D=3D 0 ) {
        thread_attachThis();	 // added
        writeln( "Child" );
    } else {
        writeln( "Failed to fork!" );
    }
}



I'm completely new to D, so this is just me speculating: By using =
low-level posix threads I think you kind of sneak behind the back of the =
D runtime. You need to give it a chance to set things up for each =
thread. This is what thread_attachThis() does. (Please correct me if I'm =
wrong.)

BR
/soul


On Jun 21, 2010, at 23:32 , Byron Heads wrote:

 When I use fork I am getting a core.thread.ThreadException: Unable to=20=
 load thread state exception.
=20
 This is dmd 2.047 on OS X
 I am trying to convert a small C application.
=20
=20
 module fork;
 import  core.sys.posix.unistd,
        std.stdio;
=20
 void main()=20
 {
    auto pid =3D fork();
=20
    if( pid > 0 ) {=20
        writeln( "Parent" );
    } else if( pid =3D=3D 0 ) {=20
        writeln( "Child" );
    } else {
        writeln( "Failed to fork!" );
    }  =20
 }
=20
=20
 $ ./fork
 Parent
 Child
 core.thread.ThreadException: Unable to load thread state
 ----------------
 5   fork                                0x00006de9 thread_suspendAll + =
85
 6   fork                                0x0000aff0=20
 D2gc3gcx3Gcx11fullcollectMFPvZk + 32
 7   fork                                0x0000afbd=20
 D2gc3gcx3Gcx16fullcollectshellMFZk + 41
 8   fork                                0x0000a0a2=20
 D2gc3gcx2GC18fullCollectNoStackMFZv + 54
 9   fork                                0x0000803c gc_term + 44
 10  fork                                0x0000cef6=20
 D2rt6dmain24mainUiPPaZi6runAllMFZv + 90
 11  fork                                0x0000cdc6=20
 D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv + 42
 12  fork                                0x0000cd54 main + 168
 13  fork                                0x00001a85 start + 53
=20
 I tried different flags but the output is the same.
Jun 22 2010