tests,port-context: explicitly close the socket that is added to the listener

Since GLib 2.42, the sockets that are added to socket listeners may no longer
be closed automatically when the listener is finalized. In order to avoid that,
we will keep our own socket reference and close/unref it ourselves.

This issue was preventing adding new test cases with the same port names.

    $ ./test-service-generic --verbose
    GTest: random seed: R02S889153ee0f2e59c570f4edff9caa4176
    GTest: run: /MM/Service/Generic/enable-disable
    Activating service name='org.freedesktop.ModemManager1'
    Successfully activated service 'org.freedesktop.ModemManager1'
    (MSG: DEBUG: client connection closed)
    (MSG: MESSAGE: Found modem at '/org/freedesktop/ModemManager1/Modem/0')
    ** Message: Found modem at '/org/freedesktop/ModemManager1/Modem/0'
    (MSG: DEBUG: client connection closed)
    GTest: result: OK
    GTest: run: /MM/Service/Generic/cme-error-detected
    Activating service name='org.freedesktop.ModemManager1'
    Successfully activated service 'org.freedesktop.ModemManager1'
    (MSG: FATAL-ERROR: Cannot bind socket: Error binding to address: Address already in use)

    ** (/home/aleksander/Development/foss/ModemManager/plugins/.libs/lt-test-service-generic:32043): ERROR **: Cannot bind socket: Error binding to address: Address already in use
This commit is contained in:
Aleksander Morgado
2016-01-16 17:32:13 +01:00
parent dfd111f291
commit 7136c0b321

View File

@@ -29,6 +29,7 @@ struct _TestPortContext {
GMutex ready_mutex;
GMainLoop *loop;
GMainContext *context;
GSocket *socket;
GSocketService *socket_service;
GList *clients;
GHashTable *commands;
@@ -280,6 +281,7 @@ create_socket_service (TestPortContext *self)
G_UNIX_SOCKET_ADDRESS_ABSTRACT));
if (!g_socket_bind (socket, address, TRUE, &error))
g_error ("Cannot bind socket: %s", error->message);
g_object_unref (address);
/* Listen */
if (!g_socket_listen (socket, &error))
@@ -297,19 +299,17 @@ create_socket_service (TestPortContext *self)
/* Start it */
g_socket_service_start (service);
/* And store it */
/* And store both the service and the socket.
* Since GLib 2.42 the socket may not be explicitly closed when the
* listener is diposed, so we'll do it ourselves. */
self->socket_service = service;
self->socket = socket;
/* Signal that the thread is ready */
g_mutex_lock (&self->ready_mutex);
self->ready = TRUE;
g_cond_signal (&self->ready_cond);
g_mutex_unlock (&self->ready_mutex);
if (socket)
g_object_unref (socket);
if (address)
g_object_unref (address);
}
/*****************************************************************************/
@@ -389,6 +389,15 @@ test_port_context_free (TestPortContext *self)
if (self->commands)
g_hash_table_unref (self->commands);
g_list_free_full (self->clients, (GDestroyNotify)client_free);
if (self->socket) {
GError *error = NULL;
if (!g_socket_close (self->socket, &error)) {
g_debug ("Couldn't close socket: %s", error->message);
g_error_free (error);
}
g_object_unref (self->socket);
}
if (self->socket_service) {
if (g_socket_service_is_active (self->socket_service))
g_socket_service_stop (self->socket_service);