Skip to content

[BUG] FBNS MQTT connection randomly disconnecting #396

Description

@qwer-lives

What broke?

I'm not sure if it's a bug, or this is expected.

I'm trying to listen to push notifications in a long lived worker. What I observe is the MQTT socket closing randomly after a while (it seems to happen around every hour but it's not consistent at all. I have tried sending pings every 10 minutes and also every 60 seconds - it doesn't seem to make any difference.

Is this expected given the nature of MQTT? If it is, should this reconnection happen internally within the aiograpi Client, or is it expected for the library user to implement their own reconnection logic?

Also, I noticed after the socket is broken calling fbns_disconnect sometimes leaves the client in a broken state: the disconnect also fails, so client.fbns is not set to None. I attached the code I have to work around this but it's not very nice.

Affected area

other

Minimal reproduction code

cl = Client()
// init client
try:
    while True:
        if not cl.fbns or not cl.fbns.connected:
            try:
                await cl.fbns_connect()
                cl.fbns_on("push", on_fbns_push)
            except Exception as e:
                log.error(f"FBNS connection error: {e}")
                await asyncio.sleep(10)
                continue
        try:
            await cl.fbns_read_once()
        except Exception as e:
            err_msg = str(e).lower()
            if "timed out" in err_msg or "timeout" in err_msg:
                continue
            log.error(f"FBNS read error: {e}")
            try:
                await cl.fbns_disconnect()
            except Exception as de:
                log.debug(f"FBNS disconnection error: {de}")
            finally:
                if cl.fbns:
                    with contextlib.suppress(Exception):
                        cl.fbns.connected = False
                        await asyncio.to_thread(cl.fbns.transport.disconnect)
                    cl.fbns = None
            await asyncio.sleep(10)
except asyncio.CancelledError:
    with contextlib.suppress(Exception):
        await cl.fbns_disconnect()
    raise
except Exception as e:
    log.exception(f"FBNS loop error: {e}")
    await asyncio.sleep(10)

Full traceback

No traceback

Observed result

MQTT socket randomly disconnects.

Expected result

Either the connection is stable, or reconnection is handled internally.

aiograpi version

1.10.26

Python version

3.14.5

Operating system

7.0.12-1-cachyos

Login method

username/password

Proxy in use

no proxy

Can you reproduce on current main?

yes

Raw API response or payload sample

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions