Skip to content

jack_client_close is locking when the jack server is periodically restarts #395

@Gravechapa

Description

@Gravechapa

Hi, i'm working on an application which must automatically reconnect to the jack server. But i'm run into some trouble when started to test it. After several times of restarting the jack server, my application ended up stuck at jack_client_close.
Here's example to reproduce the bug:

#include <jack/jack.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

jack_port_t *input_port;
jack_port_t *output_port;
jack_client_t *client;
pthread_spinlock_t spinlock;
int stop = 0;

int process (jack_nframes_t nframes, void *arg) {
    jack_default_audio_sample_t *in, *out;

    in = (jack_default_audio_sample_t *)jack_port_get_buffer (input_port, nframes);
    out = (jack_default_audio_sample_t *)jack_port_get_buffer (output_port, nframes);
    memcpy (out, in,
        sizeof (jack_default_audio_sample_t) * nframes);

    return 0;   
}

void jack_shutdown(void *arg) {
    pthread_spin_lock(&spinlock);
    stop = 1;
    pthread_spin_unlock(&spinlock);
}

void  silent_jack_error_callback(const char *msg) {}

int main() {
    pthread_spin_init(&spinlock, 0);
    jack_set_error_function(silent_jack_error_callback);
    const char *client_name = "simple";
    const char *server_name = NULL;
    jack_options_t options = JackNullOption;
    jack_status_t status;
    while(true){
    client = jack_client_open (client_name, JackNoStartServer, &status, server_name);
    if (client == NULL) {
		fprintf (stderr, "jack_client_open() failed, "
			 "status = 0x%2.0x\n", status);
		if (status & JackServerFailed) {
			fprintf (stderr, "Unable to connect to JACK server\n");
		}
		continue;
	}
    
    jack_on_shutdown(client, jack_shutdown, 0);
    jack_set_process_callback (client, process, 0);

    input_port = jack_port_register (client, "input",
                                    JACK_DEFAULT_AUDIO_TYPE,
                                    JackPortIsInput, 0);
    output_port = jack_port_register (client, "output",
                                    JACK_DEFAULT_AUDIO_TYPE,
                                    JackPortIsOutput, 0);
    
    if (jack_activate (client)) {
            fprintf (stderr, "cannot activate client");
            exit (1);
    }
    while (1) {
        pthread_spin_lock(&spinlock);
        if(stop) {
            pthread_spin_unlock(&spinlock);
            break;
        }
        pthread_spin_unlock(&spinlock);
    }
    fprintf (stderr, "stopping\n");
    jack_client_close(client);
    stop = 0;
    fprintf (stderr, "alive\n");
    }
    return 0;
}
#!/bin/bash 
while :
do 
jack_control start
sleep 1
jack_control stop
sleep 1
done

Here's stack traces of two non responding threads:

1 pthread_cond_wait * *GLIBC_2.3.2                                  0x7ffff73cdafc 
2 Jack::JackPosixProcessSync::Wait     JackPosixProcessSync.cpp 81  0x7ffff7b0350a 
3 Jack::JackMessageBuffer::Execute     JackMessageBuffer.cpp    104 0x7ffff7afc6a9 
4 Jack::JackPosixThread::ThreadHandler JackPosixThread.cpp      59  0x7ffff7b02812 
5 start_thread                                                      0x7ffff73c7a9d 
6 clone                                                             0x7ffff72f7b23 
1 __lll_lock_wait                                   0x7ffff73d0edc 
2 pthread_mutex_lock                                0x7ffff73ca336 
3 Jack::JackPosixMutex::Lock JackPosixMutex.cpp 112 0x7ffff7b03ef2 
4 Jack::JackClient::Close    JackClient.cpp     118 0x7ffff7ae0096 
5 jack_client_close          JackLibAPI.cpp     219 0x7ffff7b0772f 
6 monitor                    jack_input.c       126 0x555555558ede 
7 start_thread                                      0x7ffff73c7a9d 
8 clone                                             0x7ffff72f7b23 

Edit:
I tried to add this before jack_client_close:

 jack_deactivate(client);
 jack_port_unregister(client, input_port);
 jack_port_unregister(client, output_port);

Same result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    dbusDBus related

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions