From e97c77319728bb42d223e67cf1bf5755a80635f3 Mon Sep 17 00:00:00 2001 From: Jeanine Adkisson Date: Mon, 26 Apr 2021 12:23:42 -0400 Subject: [PATCH 1/2] Exit correctly on write errors This line (from a 9-year-old commit!) causes `alsa_in` to infinite-loop when a device is unplugged, writing `err = -11` to the standard output. It's possible `-11` could be special-cased if it represents a more specific kind of error, but the program should exit gracefully with an error code in this case. --- alsa_in.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/alsa_in.c b/alsa_in.c index 8dd13c6..4418151 100644 --- a/alsa_in.c +++ b/alsa_in.c @@ -439,8 +439,8 @@ int process (jack_nframes_t nframes, void *arg) { if( err < 0 ) { printf( "err = %d\n", err ); if (xrun_recovery(alsa_handle, err) < 0) { - //printf("Write error: %s\n", snd_strerror(err)); - //exit(EXIT_FAILURE); + printf("Write error: %s\n", snd_strerror(err)); + exit(EXIT_FAILURE); } goto again; } From d6fb8be60ef344a6c3f2479a25d9beafbfa024bd Mon Sep 17 00:00:00 2001 From: Jeanine Adkisson Date: Mon, 26 Apr 2021 14:15:59 -0400 Subject: [PATCH 2/2] Try writing 5 times before aborting --- alsa_in.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/alsa_in.c b/alsa_in.c index 4418151..d642e92 100644 --- a/alsa_in.c +++ b/alsa_in.c @@ -21,6 +21,8 @@ #include +#define XRUN_ABORT_COUNT 5 + // Here are the lists of the jack ports... JSList *capture_ports = NULL; @@ -434,15 +436,17 @@ int process (jack_nframes_t nframes, void *arg) { resample_mean = 0.9999 * resample_mean + 0.0001 * current_resample_factor; // get the data... + int loop_count = 0; again: err = snd_pcm_readi(alsa_handle, outbuf, rlen); if( err < 0 ) { - printf( "err = %d\n", err ); - if (xrun_recovery(alsa_handle, err) < 0) { - printf("Write error: %s\n", snd_strerror(err)); - exit(EXIT_FAILURE); - } - goto again; + printf( "err = %d\n", err ); + if (xrun_recovery(alsa_handle, err) < 0 && loop_count > XRUN_ABORT_COUNT) { + printf("Write error: %s\n", snd_strerror(err)); + exit(EXIT_FAILURE); + } + loop_count++; + goto again; } if( err != rlen ) { //printf( "read = %d\n", rlen );