Skip to main content

Fixed: InProcessExecution.ResumeAsync Never Returns When a Microsoft Agent Framework Workflow Has Already Finished

· 16 min read
Jagdish Kumawat
Founder @ Dewiride

I started my Microsoft Agent Framework approval app a third time, after its workflow run had already finished, and InProcessExecution.ResumeAsync never returned: no output and no error, until I pressed Ctrl+C about 5 minutes later. Resuming from the last checkpoint of a finished run waits for a halt that doesn't come. The fix is to remember the checkpoint where the run waits for its answer and resume only from that one.

What I Was Doing​

I was building a small console app, RefundApproval, in which a Microsoft Agent Framework workflow waits for an answer across restarts of the program. The project uses Microsoft.Agents.AI.Workflows 1.28.0. Its workflow asks a supervisor Refund 25 USD to the customer? through a request port, and the app saves the run's checkpoints in a checkpoints folder with FileSystemJsonCheckpointStore, under the session ID refund-1042.

Started without arguments, the app runs the workflow until it asks its question, and ends. Started with an answer, dotnet run -- y, it takes the session's latest checkpoint, resumes the run from it and answers. After the answer nothing should wait any more, so I wanted a third start to say so. I read the question with SingleOrDefault() and added a guard after ResumeAsync:

Program.cs (as it was)
using Microsoft.Agents.AI.Workflows;
using Microsoft.Agents.AI.Workflows.Checkpointing;

const string SessionId = "refund-1042";

// Checkpoints go to JSON files in the checkpoints folder, so they outlive this process.
var store = new FileSystemJsonCheckpointStore(new DirectoryInfo("checkpoints"));
CheckpointManager checkpointManager = CheckpointManager.CreateJson(store);

// Build the same workflow on every start: a checkpoint only fits the workflow that saved it.
Workflow workflow = BuildWorkflow();

if (args.Length == 0)
{
// First start: run until the workflow asks its question, then end the program.
await using Run run = await InProcessExecution.RunAsync(workflow, 25, checkpointManager, SessionId);
Console.WriteLine($"Status: {await run.GetStatusAsync()}");
Console.WriteLine($"Checkpoints saved: {run.Checkpoints.Count}");
Console.WriteLine("The program ends here. Answer later with: dotnet run -- y");
return;
}

// A later start: find the session's latest checkpoint and continue the run from it.
CheckpointInfo? latest = await checkpointManager.GetLatestCheckpointAsync(SessionId);
if (latest is null)
{
Console.WriteLine($"No checkpoints for {SessionId}. Start the request first with: dotnet run");
return;
}

await using Run resumed = await InProcessExecution.ResumeAsync(workflow, latest, checkpointManager);
Console.WriteLine($"Resumed from checkpoint {latest.CheckpointId}");
Console.WriteLine($"Status: {await resumed.GetStatusAsync()}");

// The question the first start asked comes back as a RequestInfoEvent.
RequestInfoEvent? question = resumed.OutgoingEvents.OfType<RequestInfoEvent>().SingleOrDefault();
if (question is null)
{
Console.WriteLine("Nothing is waiting for an answer.");
return;
}

question.Request.TryGetDataAs(out string? text);
Console.WriteLine($"Waiting question: {text}");

bool approved = args[0] == "y";
await resumed.ResumeAsync([question.Request.CreateResponse(approved)]);

foreach (WorkflowOutputEvent output in resumed.NewEvents.OfType<WorkflowOutputEvent>())
{
Console.WriteLine($"Output: {output.Data}");
}

Console.WriteLine($"Status: {await resumed.GetStatusAsync()}");

static Workflow BuildWorkflow()
{
RequestPort askSupervisor = RequestPort.Create<string, bool>("AskSupervisor");

Func<int, string> describe = amount => $"Refund {amount} USD to the customer?";
Func<bool, string> record = approved => approved ? "Refund approved." : "Refund rejected.";

ExecutorBinding describeRefund = describe.BindAsExecutor("DescribeRefund");
ExecutorBinding recordAnswer = record.BindAsExecutor("RecordAnswer");

return new WorkflowBuilder(describeRefund)
.AddEdge(describeRefund, askSupervisor)
.AddEdge(askSupervisor, recordAnswer)
.WithOutputFrom(recordAnswer)
.Build();
}

The first start asked the question and ended:

Terminal
dotnet run
Output
Status: PendingRequests
Checkpoints saved: 2
The program ends here. Answer later with: dotnet run -- y

The second start answered it:

Terminal
dotnet run -- y
Output
Resumed from checkpoint e727f51473e0459d958af74c95aee38e
Status: PendingRequests
Waiting question: Refund 25 USD to the customer?
Output: Refund approved.
Status: Idle

Then I started it a third time, with the same command.

What I Expected​

The third start to resume the run, find no question in it, and print Nothing is waiting for an answer..

What I Got​

Terminal
dotnet run -- y

Nothing. The program printed no line at all, not even Resumed from checkpoint, and it didn't end. About 5 minutes later I pressed Ctrl+C, and it stopped with exit code 130.

macOS-style terminal window titled Terminal — RefundApproval showing the command dotnet run -- y with no output under it, only the ^C of the Ctrl+C that stopped the program about 5 minutes later

What I Checked​

Where it stopped. Two calls come before the line that prints Resumed from checkpoint: GetLatestCheckpointAsync and ResumeAsync. One of them never returned, so my guard never ran.

The smallest program that shows it. I made a new console app, FinishedRunResume, with the same package and nothing else. Its workflow has one step that turns a text into capitals, and it runs to the end with checkpoints kept in memory. Then the program takes the session's latest checkpoint, the one saved when the run finished, and resumes from it twice: with ResumeAsync, giving it 30 seconds, and with ResumeStreamingAsync.

Program.cs (FinishedRunResume)
using Microsoft.Agents.AI.Workflows;

CheckpointManager checkpointManager = CheckpointManager.CreateInMemory();

// Run a one-step workflow to the end, with checkpoints.
await using (Run run = await InProcessExecution.RunAsync(BuildWorkflow(), "hello", checkpointManager, "session-1"))
{
WorkflowOutputEvent output = run.OutgoingEvents.OfType<WorkflowOutputEvent>().Single();
Console.WriteLine($"Run: {await run.GetStatusAsync()}, output {output.Data}, checkpoints {run.Checkpoints.Count}");
}

// Resume from the session's latest checkpoint: the one saved when the run finished.
CheckpointInfo latest = (await checkpointManager.GetLatestCheckpointAsync("session-1"))!;

Task<Run> resume = InProcessExecution.ResumeAsync(BuildWorkflow(), latest, checkpointManager).AsTask();
Task finished = await Task.WhenAny(resume, Task.Delay(TimeSpan.FromSeconds(30)));
Console.WriteLine(finished == resume ? "ResumeAsync returned." : "ResumeAsync hasn't returned after 30 seconds.");

await using StreamingRun streaming = await InProcessExecution.ResumeStreamingAsync(BuildWorkflow(), latest, checkpointManager);
Console.WriteLine($"ResumeStreamingAsync returned. Status: {await streaming.GetStatusAsync()}");

static Workflow BuildWorkflow()
{
Func<string, string> upper = text => text.ToUpperInvariant();
ExecutorBinding toUpper = upper.BindAsExecutor("ToUpper");
return new WorkflowBuilder(toUpper).WithOutputFrom(toUpper).Build();
}
Terminal
dotnet run
Output
Run: Idle, output HELLO, checkpoints 1
ResumeAsync hasn't returned after 30 seconds.
ResumeStreamingAsync returned. Status: NotStarted

The run finished with the status Idle and one checkpoint. GetLatestCheckpointAsync returned that checkpoint, so the wait was in ResumeAsync. There's no file store in this program, so the store isn't the cause either. ResumeStreamingAsync did return for the same checkpoint, with the status NotStarted.

Every checkpoint of the refund session, one by one. Next I wrote a small check program for the refund session. It opened a copy of the app's checkpoints folder, read the session's checkpoints with the store's RetrieveIndexAsync, built the workflow once, and called ResumeAsync for each checkpoint in turn, giving each call 10 seconds:

Output (shortened)
index: 6 checkpoints
#1 af093794e28d406ba8d58ec198f03a43: ResumeAsync...
returned; status PendingRequests; requests 1; events WorkflowStartedEvent,SuperStepStartedEvent,ExecutorInvokedEvent,RequestInfoEvent,ExecutorCompletedEvent,SuperStepCompletedEvent
#2 e727f51473e0459d958af74c95aee38e: ResumeAsync...
returned; status PendingRequests; requests 1; events RequestInfoEvent
#3 7a506cb5c9d845858b75fdc082df7abf: ResumeAsync...
returned; status Idle; requests 0; events WorkflowStartedEvent,SuperStepStartedEvent,ExecutorInvokedEvent,ExecutorCompletedEvent,WorkflowOutputEvent,SuperStepCompletedEvent
#4 8b2a84cf44fd45d5b58ad8d5fb4cbdf0: ResumeAsync...
TIMEOUT after 10 s
#5 e505c22f429445e6b1bb2bc5c65e7942: ResumeAsync...
Unhandled exception. System.InvalidOperationException: Cannot use a Workflow that is already owned by another runner or parent workflow.
at Microsoft.Agents.AI.Workflows.Workflow.TakeOwnership(Object ownerToken, Boolean subworkflow, Object existingOwnershipSignoff)
…

The first four are the session's own. #1 and #2 come from the first start, and #2 is the checkpoint where the run waited: the answer resumed from it. #3 and #4 come from the answer, and #4 is the last one, saved when the run finished. The index also listed two more, which an earlier pass of this check had saved: resuming #1 or #3 runs a superstep, and a checkpoint is saved at the end of every superstep.

  • #1 and #3 ran the next step and halted.
  • #2 brought back the waiting question as a RequestInfoEvent, just as the answer start had found it.
  • #4, the checkpoint of the finished run, didn't return.
  • #5 then threw InvalidOperationException, because the check reused one workflow object and the call on #4 still owned it. That's a result of the hang, not a second problem. The post on the already-owned workflow error explains the rule behind it.

What the framework's code does. Agent Framework is open source. In version 1.28.0, InProcessExecutionEnvironment.cs ends ResumeAsync like this:

InProcessExecutionEnvironment.cs (end of ResumeAsync)
Run run = new(runHandle);
await run.RunToNextHaltAsync(cancellationToken).ConfigureAwait(false);

return run;

RunToNextHaltAsync, in Run.cs, reads the run's events until their stream ends:

Run.cs (in RunToNextHaltAsync)
await foreach (WorkflowEvent evt in this._runHandle.TakeEventStreamAsync(blockOnPendingRequest: false, cancellationToken).ConfigureAwait(false))
{
hadEvents = true;
this._eventSink.Add(evt);
}

So ResumeAsync returns only after the restored run reaches its next halt, as its documentation says: The workflow will run until its first halt, and the returned Run will capture all outgoing events. ResumeStreamingAsync, in the same file, hands back the new run without this wait, which is why it returned.

Why It Happens​

GetLatestCheckpointAsync Retrieves the most recently committed checkpoint for the specified session, or null when the session has no checkpoints. Before the answer, that's the checkpoint where the run waits. After the answer, it's the checkpoint saved when the run finished, with the status Idle: no question waits in it and no step is left to run. ResumeAsync restores that run and waits for it to halt, and that wait didn't end: not in 5 minutes in the refund app, and not in 30 seconds in the smallest program. My guard came after ResumeAsync, so it never got the chance to run.

The Fix​

Don't resume from the latest checkpoint. Remember the checkpoint where the run waits for its answer, resume only from that one, and forget it when the run finishes. When nothing is remembered, nothing waits, and the app doesn't call ResumeAsync at all.

In this app a text file, waiting.txt, holds that checkpoint's ID while the run waits. Its name goes in a constant at the top of Program.cs, and the store now gets a using:

Program.cs (top)
// While the run waits for its answer, this file holds the checkpoint where it waits.
const string WaitingFile = "waiting.txt";

using var store = new FileSystemJsonCheckpointStore(new DirectoryInfo("checkpoints"));
CheckpointManager checkpointManager = CheckpointManager.CreateJson(store);

The using isn't the fix, but the store's documentation asks for it: The class implements IDisposable; callers should ensure Dispose is called to release file handles and system resources when the store is no longer needed.

When the first start stops at the question, it writes the ID of the run's last checkpoint to the file:

Program.cs (the first start)
if (args.Length == 0)
{
await using Run run = await InProcessExecution.RunAsync(workflow, 25, checkpointManager, SessionId);
Console.WriteLine($"Status: {await run.GetStatusAsync()}");

// The run waits for its answer: remember the checkpoint where it waits.
File.WriteAllText(WaitingFile, run.LastCheckpoint!.CheckpointId);
Console.WriteLine("The program ends here. Answer later with: dotnet run -- y");
return;
}

The documentation of run.LastCheckpoint says it Gets the most recent checkpoint information. Right after the run stopped at the question, that's the checkpoint where it waits. The ! tells the compiler that the value isn't null: the run saves checkpoints, because it has a checkpoint manager.

An answer start checks the file first, and resumes from the checkpoint it names:

Program.cs (an answer start)
// Resume only a run that waits. A finished run has no waiting checkpoint.
if (!File.Exists(WaitingFile))
{
Console.WriteLine("Nothing is waiting for an answer.");
return;
}

var waiting = new CheckpointInfo(SessionId, File.ReadAllText(WaitingFile));
await using Run resumed = await InProcessExecution.ResumeAsync(workflow, waiting, checkpointManager);
Console.WriteLine($"Resumed from checkpoint {waiting.CheckpointId}");

RequestInfoEvent question = resumed.OutgoingEvents.OfType<RequestInfoEvent>().Single();
  • File.Exists decides before ResumeAsync whether anything waits.
  • new CheckpointInfo(SessionId, ...) builds the checkpoint's key from the session ID and the saved checkpoint ID. The constructor's documentation: Initializes a new instance of the CheckpointInfo class with the specified session and checkpoint identifiers.
  • Single() is back: restoring the waiting checkpoint brings its question back, because, as Microsoft's guide says, When you restore from a checkpoint, any pending requests will be re-emitted as RequestInfoEvent objects.

When the answered run has finished, the last line deletes the file:

Program.cs (end of an answer start)
// The run has finished, so nothing waits any more.
File.Delete(WaitingFile);

I also dropped two lines that printed the number of checkpoints and the status right after resuming. The complete fixed file:

Program.cs
using Microsoft.Agents.AI.Workflows;
using Microsoft.Agents.AI.Workflows.Checkpointing;

const string SessionId = "refund-1042";

// While the run waits for its answer, this file holds the checkpoint where it waits.
const string WaitingFile = "waiting.txt";

using var store = new FileSystemJsonCheckpointStore(new DirectoryInfo("checkpoints"));
CheckpointManager checkpointManager = CheckpointManager.CreateJson(store);

Workflow workflow = BuildWorkflow();

if (args.Length == 0)
{
await using Run run = await InProcessExecution.RunAsync(workflow, 25, checkpointManager, SessionId);
Console.WriteLine($"Status: {await run.GetStatusAsync()}");

// The run waits for its answer: remember the checkpoint where it waits.
File.WriteAllText(WaitingFile, run.LastCheckpoint!.CheckpointId);
Console.WriteLine("The program ends here. Answer later with: dotnet run -- y");
return;
}

// Resume only a run that waits. A finished run has no waiting checkpoint.
if (!File.Exists(WaitingFile))
{
Console.WriteLine("Nothing is waiting for an answer.");
return;
}

var waiting = new CheckpointInfo(SessionId, File.ReadAllText(WaitingFile));
await using Run resumed = await InProcessExecution.ResumeAsync(workflow, waiting, checkpointManager);
Console.WriteLine($"Resumed from checkpoint {waiting.CheckpointId}");

RequestInfoEvent question = resumed.OutgoingEvents.OfType<RequestInfoEvent>().Single();
question.Request.TryGetDataAs(out string? text);
Console.WriteLine($"Waiting question: {text}");

bool approved = args[0] == "y";
await resumed.ResumeAsync([question.Request.CreateResponse(approved)]);

foreach (WorkflowOutputEvent output in resumed.NewEvents.OfType<WorkflowOutputEvent>())
{
Console.WriteLine($"Output: {output.Data}");
}

Console.WriteLine($"Status: {await resumed.GetStatusAsync()}");

// The run has finished, so nothing waits any more.
File.Delete(WaitingFile);

static Workflow BuildWorkflow()
{
RequestPort askSupervisor = RequestPort.Create<string, bool>("AskSupervisor");

Func<int, string> describe = amount => $"Refund {amount} USD to the customer?";
Func<bool, string> record = approved => approved ? "Refund approved." : "Refund rejected.";

ExecutorBinding describeRefund = describe.BindAsExecutor("DescribeRefund");
ExecutorBinding recordAnswer = record.BindAsExecutor("RecordAnswer");

return new WorkflowBuilder(describeRefund)
.AddEdge(describeRefund, askSupervisor)
.AddEdge(askSupervisor, recordAnswer)
.WithOutputFrom(recordAnswer)
.Build();
}

A text file is enough for one request on one machine. In an app with a database, keep the checkpoint ID in the request's own record instead.

After the Fix​

The same three starts, with the fixed program. The first asks the question:

Terminal
dotnet run
Output
Status: PendingRequests
The program ends here. Answer later with: dotnet run -- y

The second answers it:

Terminal
dotnet run -- y
Output
Resumed from checkpoint e3eaaf824e874a3aab94e0c22a42779a
Waiting question: Refund 25 USD to the customer?
Output: Refund approved.
Status: Idle

The third finds nothing to resume:

Terminal
dotnet run -- y
Output
Nothing is waiting for an answer.

macOS-style terminal window titled Terminal — RefundApproval showing dotnet run -- y printing Nothing is waiting for an answer. after the run has finished

The third start printed its message and ended, where before it never ended. The first two starts ask and answer as before, without the two lines I dropped.

Common Mistakes​

MistakeWhat happensDo this instead
Resuming from GetLatestCheckpointAsync after the run has finishedResumeAsync doesn't returnResume only from the checkpoint where the run waits
Looking for the question after ResumeAsync to find out whether anything waitsThe check never runs, because ResumeAsync doesn't return for a finished runDecide before you resume, as the File.Exists check does
Keeping the waiting checkpoint after the run has finishedResuming it still brings the question back, as #2 did in the check, so an answer would run the steps after the question againForget the checkpoint as soon as the run finishes
Retrying ResumeAsync on the same workflow object after a call that didn't returnInvalidOperationException: the earlier call still owns the workflowAvoid the call that doesn't return

Troubleshooting​

SymptomLikely causeFix
The program prints nothing after ResumeAsync and doesn't endThe checkpoint is the last one of a finished runResume from the checkpoint where the run waits
ResumeStreamingAsync returns with the status NotStartedThe same kind of checkpointThe same fix
Cannot use a Workflow that is already owned by another runner or parent workflow. after a call that timed outThe call that didn't return still owns the workflow objectFix the hang; the smallest program used a new workflow object for its next call

FAQ​

Can't I just give ResumeAsync a timeout?​

A timeout stops your code from waiting, not the call. In the check, the call on #4 still owned the workflow after its 10 seconds were up, and the next call failed because of it. Check whether anything waits before you call ResumeAsync instead.

Does the latest checkpoint ever work?​

Yes, while the run still waits and nothing has run since: then the latest checkpoint is the one where the run waits, as the second start showed. Once the answer has run, the latest checkpoint is a newer one. When a step after the answer fails, that newer checkpoint has no question in it although the run never finished, which is why the guide to resuming from the checkpoint where the run waits keeps that checkpoint from the start.

Conclusion​

After a workflow run has finished, the latest checkpoint of its session is the one saved at its end, and InProcessExecution.ResumeAsync from that checkpoint waits for a halt that never comes: my third start printed nothing until I pressed Ctrl+C. Keep the ID of the checkpoint where the run waits for its answer, resume only from that one, and forget it when the run finishes. With that change, the third start printed Nothing is waiting for an answer. and ended.

Additional Resources​