Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

100% CPU usage when sxiv is compiled with AUTORELOAD=nop and run in background in a subshell #297

Closed
iamleot opened this issue Dec 10, 2017 · 3 comments

Comments

@iamleot
Copy link
Contributor

iamleot commented Dec 10, 2017

Running v24 compiled with AUTORELOAD=nop in background in a subshell leads
to 100% CPU usage due a lot of select(2) calls, e.g.:

$ ( sxiv 16x16.png & )

Possible interesting lines from ktruss(1):

$ ktruss -id sh -c '( sxiv 16x16.png & )'
[...]
 10081      1 sxiv     __gettimeofday50(0x7f7fff79fce0, 0) = 0
 10081      1 sxiv     recvmsg(0x3, 0x7f7fff79fba0, 0) Err#35 EAGAIN
 10081      1 sxiv     __select50(0x4, 0x7f7fff79fd30, 0, 0, 0x7f7fff79fd20) = 1
 10081      1 sxiv     __gettimeofday50(0x7f7fff79fce0, 0) = 0
 10081      1 sxiv     recvmsg(0x3, 0x7f7fff79fba0, 0) Err#35 EAGAIN
 10081      1 sxiv     __select50(0x4, 0x7f7fff79fd30, 0, 0, 0x7f7fff79fd20) = 1
 10081      1 sxiv     __gettimeofday50(0x7f7fff79fce0, 0) = 0
 10081      1 sxiv     recvmsg(0x3, 0x7f7fff79fba0, 0) Err#35 EAGAIN
 10081      1 sxiv     __select50(0x4, 0x7f7fff79fd30, 0, 0, 0x7f7fff79fd20) = 1
 10081      1 sxiv     __gettimeofday50(0x7f7fff79fce0, 0) = 0
 10081      1 sxiv     recvmsg(0x3, 0x7f7fff79fba0, 0) Err#35 EAGAIN
 10081      1 sxiv     __select50(0x4, 0x7f7fff79fd30, 0, 0, 0x7f7fff79fd20) = 1
 10081      1 sxiv     __gettimeofday50(0x7f7fff79fce0, 0) = 0
 10081      1 sxiv     recvmsg(0x3, 0x7f7fff79fba0, 0) Err#35 EAGAIN
 10081      1 sxiv     __select50(0x4, 0x7f7fff79fd30, 0, 0, 0x7f7fff79fd20) = 1
 10081      1 sxiv     __gettimeofday50(0x7f7fff79fce0, 0) = 0
[...]

This only happens when sxiv is run in the background and in a subshell, there
aren't any problem running sxiv via:

$ sxiv 16x16.png
$ sxiv 16x16.png &
$ ( sxiv 16x16.png )

Unfortunately I have not figured out why that happens but the following patch
(tested with v24, but should cleanly apply also on the master branch, if
not please let me know and I'll adjust it!):

--- autoreload_nop.c.orig	2017-10-27 15:25:56.000000000 +0000
+++ autoreload_nop.c
@@ -20,7 +20,7 @@
 
 void arl_init(arl_t *arl)
 {
-	(void) arl;
+	arl->fd = -1;
 }
 
 void arl_cleanup(arl_t *arl)

Please note that I have only noticed that on NetBSD, not sure if also other
platforms are affected.

Do not hesitate to contact me if more information, further debugging, etc. is
needed!

Thanks for the attention!

@sdx23
Copy link
Contributor

sdx23 commented Dec 10, 2017

Thank you for your report. This bug is probably my fault, sorry for that.

Please note that I have only noticed that on NetBSD, not sure if also other platforms are affected.

Reproduced the behaviour as described with debian jessie, on master. Indeed, check_timeouts() and arl_handle() will be called repeatedly if sxiv is called non-interactive, i.e. the same will happen for echo "" | ./sxiv timage.png.

The patch suggested is exactly what is needed. Without, arl->fd is 0, thus resembling stdin. In both suggested non-interactive modes there is data available to be read from stdin and thus FD_ISSET true. Same happens when you start sxiv normally and then press e.g. enter in the terminal window. In the originally reported setting stdin is actually /dev/null (see ls -alh /proc/self/fd/0) which provides EOF to be read.

xyb3rt pushed a commit that referenced this issue Dec 13, 2017
When sxiv is built with AUTORELOAD=nop if something is feed via stdin it
will start to infinitely call select() leading to a 100% CPU usage.

Thanks to @sdx23 for helping in analyzing that problem!

Fixes issue #297.
@xyb3rt
Copy link
Owner

xyb3rt commented Dec 13, 2017

Thanks for reporting and fixing this bug.

@xyb3rt xyb3rt closed this as completed Dec 13, 2017
@iamleot
Copy link
Contributor Author

iamleot commented Dec 13, 2017 via email

cdown pushed a commit to cdown/sxiv that referenced this issue Jul 23, 2022
Closes: xyb3rt#297

Co-authored-by: Sudo Nice <[email protected]>
Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/347
Reviewed-by: NRK <[email protected]>
Co-authored-by: zood <[email protected]>
Co-committed-by: zood <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants