[sane-devel] pthread_detach after pthread_join generates segfault
gerhard at gjaeger.de
Thu Aug 27 06:52:42 UTC 2015
honestly I don't recall, why I have added that. But it seems incorrect
to me now and superfluous. In fact it should make sure to cleanup the
resources used by a thread. IMO it should be save to remove pthread_detach()
or like you suggested, call it only in case pthread_join() fails.
I wonder why it took 11 years to spot that ;)
On Tuesday 25 August 2015 20:53:11 m. allan noah wrote:
> I'm no expert either, but your basic analysis seems correct- once you
> have returned from pthread_join(), the thread is gone, and its return
> value has been popped. I suppose it is possible that the pthread_join
> could fail in a way that might leave the thread running, perhaps
> because of a deadlock? In that case I suppose we could call
> pthread_detatch only if there is an error?
> Gerhard- do you remember anything about commit
> 0fb34bb098f1cbdd9d154328cb7f9d45ed8f6256 ?
> On Tue, Aug 25, 2015 at 5:36 PM, Luiz Angelo Daros de Luca
> <luizluca at gmail.com> wrote:
> > Hello,
> > I was getting a segfault for sane-backends when using device sane-test. This
> > happened when I compiled sane with uclibc or musl (on openwrt).
> > I debugged the problem and traced it to sanei_thread_waitpid (at
> > sanei/sanei_thread.c). It seems that calling pthread_detach after someone
> > already did a pthread_join causes a segfault.
> > I'm no pthread expert but I guess that after a pthread_join, the thread
> > becomes "detached" and "terminated".
> > And, according to man page PTHREAD_DETACH(3):
> > "Attempting to detach an already detached thread results in unspecified
> > behavior."
> > I simply removed these detach lines in order to solve my problem. What would
> > be the correct fix? Is there any case when join fails and detach is still
> > valid? I checked error codes and only EDEADLK(when the thread is already
> > waiting for the caller) and EINVAL (when three is another thread is already
> > waiting for it) are possible situations when detach might do something. Are
> > they possible in sane thread usage? If so, I guess detach might only be
> > called when one of the possible fails of join happens.
> > Regards,
> > Luiz Angelo Daros de Luca
> > --
> > sane-devel mailing list: sane-devel at lists.alioth.debian.org
> > http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/sane-devel
> > Unsubscribe: Send mail with subject "unsubscribe your_password"
> > to sane-devel-request at lists.alioth.debian.org
More information about the sane-devel