Skip to content

Support reconnecting Greengrass IPC clients with a refreshed auth token #697

Open

Description

@boeboe

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:

  1. greengrasscoreipc.connect(authtoken=...) should also accept a callable that returns the token. The callable is called on every (re)connect.
  2. GreengrassCoreIPCClient (and through .client also GreengrassCoreIPCClientV2) should have a public reconnect() 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 builds MessageAmendment.create_static_authtoken_amender(authtoken). After a nucleus restart the process may receive a new token (for example a new SVCUID written by the component's lifecycle scripts), but a reconnect would still send the old one and get AccessDeniedError.
  • eventstreamrpc.Connection already supports reconnecting by calling connect() again, and it calls its connect_message_amender on 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:

  • authtoken in greengrasscoreipc.connect() accepts str or Callable[[], str].
  • New MessageAmendment.create_authtoken_amender(authtoken_provider); the static amender delegates to it. The payload is now built with json.dumps, so tokens containing " or \ produce valid JSON.
  • New Connection.reconnect() and Client.reconnect(), reusing the lifecycle handler of the last connect().
  • 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 subscriptions

Other 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions