Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 15, 2026

Describe the change
Long-running hosts using the in-memory persistence provider retain terminated workflows indefinitely, leading to accumulating instance history. This adds purge support so terminal workflows can be cleared from memory as expected.

  • Memory purge behavior: Added purge support for terminal (completed/terminated) workflows and their subscriptions in the in-memory store.
  • Coverage: Added unit tests that verify purge removes terminal workflows while keeping active ones.

Describe your implementation or design

  • Purger wiring: Implemented and registered an in-memory IWorkflowPurger aligned with existing provider patterns.
  • Tests: Updated memory persistence tests to use async/await and cover purge scenarios.
await purger.PurgeWorkflows(WorkflowStatus.Terminated, DateTime.UtcNow.AddMinutes(-1), 100);

Tests
Not run (requires .NET 6 runtime for net6.0 targets in this environment).

Breaking change
No.

Additional context
None.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.example.com
    • Triggering command: /opt/oracle/product/26ai/dbhomeFree/bin/oracle oracleFREE (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq))) --bundle /run/containerd/io.containerd.runtime.v2.task/moby/6509a570663d48122afa393102abb172.17.0.4 --pid-file /run/containerd/io.containerd.runtime.v2.task/moby/6509a570663d48122afa393102abb1521 6509a570663d48122afa393102abb66444aa9f52d78a6b18172be33803ad2e6b ep/bin/linux-x64/rg (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>host.PersistenceStore memory always exists after workflow terminates</issue_title>
<issue_description>- I can start work flow host in web api main program by call app.StartHost() method:

var builder = WebApplication.CreateBuilder(args);
Log.Information("Starting web host");
var app = builder.Build();
try
{
  app.StartHost();
  app.Run();
}
finally
{
  Log.CloseAndFlush();
}
  • StartHost method implemnts:
public static WebApplication StartHost(this WebApplication app)
{
  var services = app.Services;
  var log = app.Services.GetRequiredService<ILoggerFactory>().CreateLogger("WorkflowStartup");
  var registry = services.GetService<IWorkflowRegistry>();
  var host = app.Services.GetRequiredService<IWorkflowHost>();
  if (registry is not null && !registry.IsRegistered(nameof(StartWorkflow), 1))
  {
    host.RegisterWorkflow<StartWorkflow, WorkflowData<string>>();
    host.RegisterWorkflow<PauseWorkflow, PauseWorkflowData>();
    log.LogInformation("Registered Workflow ");
  }
  LifeCycleEventHandler lifeCycleHandler = async evt =>
  {
    try
    {
      if (evt is WorkflowCore.Models.LifeCycleEvents.WorkflowError stepErr)
      {
        log.LogError(
          "Workflow {Id} step {StepId} error: {Message}",
          stepErr.WorkflowInstanceId,
          stepErr.StepId,
          stepErr.Message
        );
        var z = host.PersistenceStore;
        await host.TerminateWorkflow(evt.WorkflowInstanceId);
      }
    }
    catch
    {
      log.LogError("Error handling life cycle event");
    }
  };

  host.OnLifeCycleEvent += lifeCycleHandler;
  log.LogInformation("Starting WorkflowCore host...");
  host.Start();

  app.Lifetime.ApplicationStopping.Register(() =>
  {
    host.OnLifeCycleEvent -= lifeCycleHandler;
    host.Stop();
  });
  • i can start the work flow in the service from DI container as below
    var workflowId = await host.StartWorkflow(
      nameof(StartWorkflow),
      new JobWorkflowData<string>() 
    );

Question:
The host and work flow started normally. it is expected action that when error occurs ,then the workflow was terminated, then i can start the work flow again.
But i found that the inner instances of host persistenceStore are added after per work flow terminated , i have a bit worry about the memory will up with time passes, I want to know how to clear terminated history workflow instances?

below is the memory snapshot ( inner instances of host persistenceStore):
Image

</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 15, 2026 01:38
Co-authored-by: danielgerlag <2357007+danielgerlag@users.noreply.github.com>
Co-authored-by: danielgerlag <2357007+danielgerlag@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix host.PersistenceStore memory retention after workflow termination Add purge support for in-memory persistence Jan 15, 2026
Copilot AI requested a review from danielgerlag January 15, 2026 01:48
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.

host.PersistenceStore memory always exists after workflow terminates

2 participants