Repository navigation
Description
Describe the feature
Make it possible to reconnect a Greengrass IPC client after the nucleus has closed the connection, with an auth token that may have changed in the meantime:
greengrasscoreipc.connect(authtoken=...)should also accept a callable that returns the token. The callable is called on every (re)connect.GreengrassCoreIPCClient(and through.clientalsoGreengrassCoreIPCClientV2) should have a publicreconnect()method.
Use Case
A Greengrass IPC client never reconnects by itself. Once the nucleus closes the IPC socket (for example because the nucleus restarts), every new operation fails with ConnectionClosedError.
That is fine for a component that the nucleus starts as a child process, because the nucleus restarts it as well. But some processes use IPC without being restarted by the nucleus. One example is a long-running service managed by systemd, which must keep running (and keep doing its local work) while the nucleus restarts or the device is offline. Such a process has to reconnect by itself, and today the public API makes that hard:
greengrasscoreipc.connect()always buildsMessageAmendment.create_static_authtoken_amender(authtoken). After a nucleus restart the process may receive a new token (for example a newSVCUIDwritten by the component's lifecycle scripts), but a reconnect would still send the old one and getAccessDeniedError.eventstreamrpc.Connectionalready supports reconnecting by callingconnect()again, and it calls itsconnect_message_amenderon every connect. But the IPC client only holds the connection as the private_connection, so applications end up creating a brand-new client (with its own event loop group and executor) for every reconnect.
Proposed Solution
Implemented in #696, fully backwards compatible and without touching code-generated files:
authtokeningreengrasscoreipc.connect()acceptsstrorCallable[[], str].- New
MessageAmendment.create_authtoken_amender(authtoken_provider); the static amender delegates to it. The payload is now built withjson.dumps, so tokens containing"or\produce valid JSON. - New
Connection.reconnect()andClient.reconnect(), reusing the lifecycle handler of the lastconnect(). - The
connect()docstring explains that the client does not reconnect by itself and that streaming operations must be created again after a reconnect.
ipc = greengrasscoreipc.connect(
authtoken=lambda: Path("/path/to/svcuid").read_text(), # read again on every (re)connect
lifecycle_handler=MyHandler(), # on_disconnect schedules a reconnect on the app's own thread
)
...
ipc.reconnect().result(10) # then re-create subscriptionsOther Information
A possible follow-up is an opt-in automatic reconnect with back-off that also re-creates streaming operations. That is a larger behavioural change, so #696 only adds the building blocks applications need to do it themselves.
Acknowledgements
- I may be able to implement this feature request
- This feature might incur a breaking change
SDK version used
1.31.0 (also verified against main)
Environment details (OS name and version, etc.)
Linux (Raspberry Pi OS / Debian), Greengrass nucleus 2.x, Python 3.9+, awscrt 0.36–0.38
Activity