Skip to content

(ios) Bug fix for inter-process communication#776

Merged
dpad85 merged 4 commits intomasterfrom
ios-bg-fg-communication
Nov 5, 2025
Merged

(ios) Bug fix for inter-process communication#776
dpad85 merged 4 commits intomasterfrom
ios-bg-fg-communication

Conversation

@robbiehanson
Copy link
Contributor

@robbiehanson robbiehanson commented Nov 5, 2025

TLDR:

I broken some inter-process communication stuff during the refactor for multiple-wallet support. This PR fixes the issue.

Full story:

On iOS, there are 2 separate processes:

  • mainApp (which is the UI app)
  • notifySrvExt (a "notification service extension" which is launched automatically by iOS)

These are 2 separate apps, with separate peer instances, which might connect and receive a payment.

In general, here's how it works:

  • when a push notification arrives for Phoenix, iOS asks the question "is the mainApp open AND in the foreground"
  • if the answer is YES, then the push notification is handed to the mainApp for processing
  • if the answer is NO, then the notifySrvExt process is launched, and is handed the push notification for processing
  • the notifySrvExt is granted a maximum of 30 seconds to finish running, or it will be killed automatically by iOS

This means that most of the time, the 2 processes are not running at the same time. But obviously, there are edge cases:

  • the user might launch the mainApp while the notifySrvExt is running
  • the user might bring the mainApp into the foreground while the notifySrvExt is running (i.e. mainApp was already open, it was just in the background when push notification arrived)

We cannot have both running at the same time, since they will interfere with each other. (Cause disconnects and/or force-closed channels.)

So to properly handle these edge cases, we use a bit of inter-process communication. The basic idea is:

  • there is a simple mechanism for each process to detect if the other is running
  • if the notifySrvExt process detects the mainApp is running, it will generally abort
  • unless the notifySrvExt process is already connected to the peer (because in this case, it might have already started processing the incoming payment)
  • so when the notifySrvExt process connects to the peer, it will continually (every 2 seconds) update a flag/timestamp in the shared UserDefaults system
  • the mainApp checks this flag/timestamp, and when it detects the timestamp is recent (less than 5 seconds ago), it will prevent the peer from connecting

This system is pretty durable, because it continues to work even if the notifySrvExt process crashes. That is, if the notifySrvExt process crashes, it will stop updating the timestamp, and it will eventually become more than 5 seconds old, allowing the mainApp to reconnect.

So what did I break:

  • the mainApp has a StateFlow that should fire when the flag/timestamp is updated via the notifySrvExt. I broke this. So it never fired, and the inter-process communication was broken.

@robbiehanson robbiehanson force-pushed the ios-bg-fg-communication branch from ad7ae09 to fc8907d Compare November 5, 2025 16:20
@robbiehanson robbiehanson marked this pull request as ready for review November 5, 2025 16:20
@robbiehanson robbiehanson requested a review from dpad85 November 5, 2025 16:33
@dpad85 dpad85 merged commit d2e18da into master Nov 5, 2025
@dpad85 dpad85 deleted the ios-bg-fg-communication branch November 5, 2025 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants