cosmopolitan icon indicating copy to clipboard operation
cosmopolitan copied to clipboard

Fix signal handling under Rosetta

Open dkulchenko opened this issue 2 years ago • 0 comments

I'll preemptively mark this one as community-supported, but wanted to seek input:

Ctrl-C or Ctrl-D to quit Redbean doesn't work correctly under Rosetta. On non-assimilated binaries (running under ape), issuing Ctrl-C or Ctrl-D hangs the process and creates a rapid memory leak (used 64GB of RAM in about 10 seconds) which doesn't stop until I killall -9 .ape. On assimilated binaries, there is no memory leak and we see this instead:

I2022-06-27T17:40:54.089084:tool/net/redbean.c:6930:redbean:89097] (srvr) listen http://127.0.0.1:8080
I2022-06-27T17:40:54+000928:tool/net/redbean.c:6930:redbean:89097] (srvr) listen http://192.168.1.187:8080
I2022-06-27T17:40:54+000026:tool/net/redbean.c:6930:redbean:89097] (srvr) listen http://10.147.19.71:8080
>:
rosetta error: unsupported privilege level: 0
[1]    89097 trace trap  ./redbean.com
❯

(that's for a Ctrl-C). And this for a Ctrl-D:

I2022-06-27T17:41:16.856741:tool/net/redbean.c:6930:redbean:89110] (srvr) listen http://127.0.0.1:8080
I2022-06-27T17:41:16+000815:tool/net/redbean.c:6930:redbean:89110] (srvr) listen http://192.168.1.187:8080
I2022-06-27T17:41:16+000020:tool/net/redbean.c:6930:redbean:89110] (srvr) listen http://10.147.19.71:8080
>:
I2022-06-27T17:41:17.399988:tool/net/redbean.c:6952:redbean:89110] (srvr) received SIGHUP
I2022-06-27T17:41:17+000020:tool/net/redbean.c:6955:redbean:89110] (srvr) killing process group
rosetta error: unsupported privilege level: 0
[1]    89110 trace trap  ./redbean.com
❯

I've tracked the issue down to signal handling - if I comment out SigInit(); in redbean.c, there are no errors or leaks on both assimilated and non-assimilated binaries. Here's a Ctrl-D:

I2022-06-27T17:42:53.387199:tool/net/redbean.c:6930:redbean:89253] (srvr) listen http://127.0.0.1:8080
I2022-06-27T17:42:53+000892:tool/net/redbean.c:6930:redbean:89253] (srvr) listen http://192.168.1.187:8080
I2022-06-27T17:42:53+000023:tool/net/redbean.c:6930:redbean:89253] (srvr) listen http://10.147.19.71:8080
>:
I2022-06-27T17:42:54.462608:tool/net/redbean.c:6952:redbean:89253] (srvr) received SIGHUP
I2022-06-27T17:42:54+000045:tool/net/redbean.c:6955:redbean:89253] (srvr) killing process group
[1]    89253 terminated  sh ./redbean.com
❯

(but of course, now we're not catching signals so we're not doing proper cleanup here). I'm guessing the issue is in either __sigaction or sigenter.xnu.c, perhaps the flags or mask need to be tweaked, but I'm not really sure where to begin with those - @jart any suggestions on what might be worth trying?

dkulchenko avatar Jun 28 '22 00:06 dkulchenko