2006-10-01 Havoc Pennington <hp@redhat.com>
+ * dbus/dbus-bus.c
+ (internal_bus_get): only weak ref the connection; this means
+ _dbus_bus_notify_shared_connection_disconnected_unlocked can be
+ called safely in any context
+ (_dbus_bus_notify_shared_connection_disconnected_unlocked): don't
+ unref
+
+ * dbus/dbus-connection.c
+ (_dbus_connection_get_dispatch_status_unlocked): move
+ _dbus_bus_notify_shared_connection_disconnected_unlocked here
+ when queuing Disconnected instead of when the Disconnected message
+ arrives, so dbus_bus_get() won't return closed connections.
+
+2006-10-01 Havoc Pennington <hp@redhat.com>
+
* dbus/dbus-connection.c (_dbus_connection_close_if_only_one_ref):
Add a hack to make DBusNewConnectionFunction work right.
* message hasn't been processed yet, in which case we
* want to pretend it isn't in the hash and avoid
* returning it.
+ *
+ * The idea is to avoid ever returning a disconnected connection
+ * from dbus_connection_open(). We could just synchronously
+ * drop our shared ref to the connection on connection disconnect,
+ * and then assert here that the connection is connected, but
+ * that causes reentrancy headaches.
*/
CONNECTION_LOCK (connection);
if (_dbus_connection_get_is_connected_unlocked (connection))
connection->server_guid = NULL;
_DBUS_UNLOCK (shared_connections);
}
-
- _dbus_bus_notify_shared_connection_disconnected_unlocked (connection);
/* remove our reference held on all shareable connections */
_dbus_connection_unref_unlocked (connection);
_dbus_connection_last_unref (connection);
}
+/*
+ * Note that the transport can disconnect itself (other end drops us)
+ * and in that case this function never runs. So this function must
+ * not do anything more than disconnect the transport and update the
+ * dispatch status.
+ *
+ * If the transport self-disconnects, then we assume someone will
+ * dispatch the connection to cause the dispatch status update.
+ */
static void
_dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection)
{
_dbus_transport_disconnect (connection->transport);
- _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
+ /* This has the side effect of queuing the disconnect message link
+ * (unless we don't have enough memory, possibly, so don't assert it).
+ * After the disconnect message link is queued, dbus_bus_get/dbus_connection_open
+ * should never again return the newly-disconnected connection.
+ *
+ * However, we only unref the shared connection and exit_on_disconnect when
+ * the disconnect message reaches the head of the message queue,
+ * NOT when it's first queued.
+ */
status = _dbus_connection_get_dispatch_status_unlocked (connection);
- /* this calls out to user code */
+ /* This calls out to user code */
_dbus_connection_update_dispatch_status_and_unlock (connection, status);
- /* could also call out to user code */
+ /* Could also call out to user code */
dbus_connection_unref (connection);
}
if (!is_connected)
{
+ /* It's possible this would be better done by having an
+ * explicit notification from
+ * _dbus_transport_disconnect() that would synchronously
+ * do this, instead of waiting for the next dispatch
+ * status check. However, probably not good to change
+ * until it causes a problem.
+ */
+
if (status == DBUS_DISPATCH_COMPLETE &&
connection->disconnect_message_link)
- {
+ {
_dbus_verbose ("Sending disconnect message from %s\n",
_DBUS_FUNCTION_NAME);
connection->disconnect_message_link);
connection->disconnect_message_link = NULL;
+ /* Set the weakref in dbus-bus.c to NULL, so nobody will get a disconnected
+ * connection from dbus_bus_get(). We make the same guarantee for
+ * dbus_connection_open() but in a different way since we don't want to
+ * unref right here; we instead check for connectedness before returning
+ * the connection from the hash.
+ */
+ _dbus_bus_notify_shared_connection_disconnected_unlocked (connection);
+
status = DBUS_DISPATCH_DATA_REMAINS;
}