[sane-devel] scanimage hangs at shutdown (Preparing for a sane-backends release)

Olaf Meeuwissen olaf.meeuwissen at avasys.jp
Wed Aug 19 01:27:28 UTC 2015

Rolf Bensch writes:

> Using fork or pthread makes no difference.
> A sleep at the end of main() works wonders.
> But not for the other exit() functions. 'scanimage -h' or 'scanimage -A'
> still hang with the same issue.
> I suggest to replace atexit and all exit() functions by the existing
> scanimage_exit() function.
> Please check if attached patch is running with your installations.

I had a look at the patch and tripped over the fixme.  Next, I had a
look at the scanimage_exit() implementation and the timing of adding
that to the list of functions to call atexit.  Something definitely
smells there.

 - scanimage_exit() may call sane_close().  That function may need to do
   device I/O.
 - scanimage_exit() is register with atexit() *before* sane_init() gets
   called.  This means that any functions registered via atexit() in a
   backend run *before* scanimage_exit().
 - none of the backends call atexit() directly but any library they use
   may register exit handlers

Is there any chance that the libusb-compat installations that are
affected register an exit handler?  Say something like libusb_exit()?
If that is the case, device I/O will be made impossible *before*
scanimage_exit() gets a chance to sane_close() the device.

Note that the -h and -A options to scanimage need the device's options
so that just about guarantees that its backend needs to talk to it.

Does moving the atexit() call *after* sane_init() fix this issue?

Hope this helps,
Olaf Meeuwissen, LPIC-2     FLOSS Engineer -- EPSON AVASYS CORPORATION
FSF Associate Member #1962               Help support software freedom

More information about the sane-devel mailing list