GDBus : connection is closed error

I tried to establish a Peer to Peer DBus communication by following the C example included in Gio.
import gi
from gi.repository import Gio, GLib

xml = (
    "  <interface name='org.gtk.GDBus.TestPeerInterface'>"
    "    <method name='HelloWorld'>"
    "      <arg type='s' name='greeting' direction='in'/>"
    "      <arg type='s' name='response' direction='out'/>"
    "    </method>"
    "  </interface>"

node = Gio.DBusNodeInfo.new_for_xml(xml)
loop = GLib.MainLoop()

def allow_mechanism(observer, mechanism):

    return True

def authorize(observer, stream, client_credentials):

    server_credentials = Gio.Credentials()
    print("Client\n", client_credentials.to_string())
    print("Server\n", server_credentials.to_string())

    if server_credentials.is_same_user(client_credentials):
        return True
    return True

def connection_closed(connection, vanished):

    print("Connection closed ", vanished)

def handle_method_call(
    connection, sender, object_path, interface_name, method_name, params, invocation

    if method_name == "HelloWorld":
        print("Got a 'HelloWorld'")
        msg = params.unpack()[0]
        greeting = GLib.Variant(
            "(s)", (f"Got message : {msg}",))

def new_connection(server, connection):

    credentials = connection.get_peer_credentials()
    caps = connection.get_capabilities()
    print("Credentials\n", credentials.to_string())
    print("Capabilities\n", int(caps))

    connection.connect("closed", connection_closed)
    reg_id = connection.register_object(
        "/org/gtk/GDBus/TestObject", node.interfaces[0], handle_method_call, None, None

    assert reg_id > 0
    return True

address = "unix:abstract=myaddr"
flags = Gio.DBusServerFlags.NONE
guid = Gio.dbus_generate_guid()
observer = Gio.DBusAuthObserver()

observer.connect("allow-mechanism", allow_mechanism)
observer.connect("authorize-authenticated-peer", authorize)

server = Gio.DBusServer.new_sync(address, flags, guid, observer)

print("Client addresss ", server.get_client_address())
server.connect("new-connection", new_connection)
except KeyboardInterrupt:
from gi.repository import Gio, GLib

connection = Gio.DBusConnection.new_for_address_sync("unix:abstract=myaddr",

var = GLib.Variant("(s)", ("Hi there",))
val = connection.call_sync(

After launching server, whenever I execute, I get connection is closed error. Why does it happen? Who is closing the connection?

Please help me.

Hey @j_arun_mani,

First things first: are you sure that your use case warrants setting up your own D-Bus server? If you just need a client and a server to communicate, it’s better to use the session or system bus (available through g_bus_get(), among others). It’s also much easier to setup.

Next, I’m not terribly familiar with GDBusServer, but if I look at your code I see some strange things:

  • You make an observer in, but don’t use it anywhere
  • You specify in the client that authentication should be used (Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT), but don’t provide an observer
1 Like

Hi there

I’m doing what is said in the C example, just directly without any change. I had the same doubt of implementing custom Connection etc. but seems like there is no other way to design a private bus.

Oh yes, sorry for the strange things, I updated the code but pasted the old one here, let me update it.

Code updated, same issue.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.