square / square/workflow-swift

Workflow host observers are notified before internal updates are finished

Open
#289 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Swift
Stars
373
Forks
46
Avg merge
6h 38m
Merged PRs (30d)
3

Description

Scenario

Say I am hosting a Workflow in a UIViewController using a WorkflowHostingController. In the view controller, I am observing the output of my Workflow with something like:

hostingController.output.take(during: lifetime).observeValues { [weak self] output in
    guard let self else { return }

    switch output {
    case .somethingChanged(let foo):
        self.foo = foo
    }
}

If I need to pass the new value of foo back into the Workflow, and I try to update the Workflow synchronously, e.g. by calling:

hostingController.update(workflow: MyWorkflow(foo: foo))

then this consistently results in a crash: Fatal error: EventPipe can only be enabled from the `pending` state

A suggestion by @jamieQ is that "we could potentially delay notifying the workflow host observers until after all the internal updates were finished."

Workarounds

We can avoid the crash by delaying event handling, either when observing the Workflow output:

hostingController.output.take(during: lifetime).observe(on: QueueScheduler.main).observeValues {
    // ...
}

or when updating the Workflow:

DispatchQueue.main.async { [weak self] in
    guard let self else { return }
    self.hostingController.update(workflow: MyWorkflow(foo: foo))
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the synchronous output observation and WorkflowHostingController.update(workflow:) sequence described in the issue, then inspect the WorkflowHostingController notification flow and EventPipe state transitions. Done means a host observer can synchronously update the Workflow without the “EventPipe can only be enabled from the pending state” crash.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.