Hi folks,

while trying to use avarice with the --detach option, I ran into read
timeouts, whereas everything works without --detach (using a JTAGICE3).
Someone mentioned this before on this list (IIRC), but didn't look for
the real cause. I decided to dig in.

I've found that the actual act of forking directly breaks all
communication. If I moved the forking a lot up to before the creation of
the jtag3 object, things still worked. Moving it down until after the
creation (but before the initJtagBox call) broke the initial signon
command with a read timeout.

Digging further, I've found that the USB communication, as initialized
in jtag::openUSB, uses pthreads. It seems these do not play well with
fork():

        The fork(2) function creates a copy of the process, all memory
        pages are copied, open file descriptors are copied etc. All this
        stuff is intuitive for a UNIX programmer. One important thing
        that differs the child process from the parent is that the child
        has only one thread.

From 
http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them

So the problem seems obvious: forking after creating threads does not
work. I tried re-creating the threads after the fork, which appears to
work, but gave an error at shutdown:

*** Error in `avarice': double free or corruption (fasttop): 0x000000000086a730 
***

That's probably libusb that doesn't like being cut short halfway through
a blocking call.


It seems the proper solution here is to fork early, and then let the
parent process wait for SIGUSR1 before exiting (and letting the forked
process send that signal after initialization is complete). Does that
sound reasonable?

It seems this is similar what the pre-pthread implementation did, except
that that used a child process for just the usb communication and a
persistent parent process, and this would just be short-term
arrangement, with the child process doing all the work.

How does this sound?

Gr.

Matthijs

Attachment: signature.asc
Description: Digital signature

------------------------------------------------------------------------------
_______________________________________________
avarice-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/avarice-user

Reply via email to