3

I have a windows service, running workflows. The workflows are XAMLs loaded from database (users can define their own workflows using a rehosted designer). It is configured with one instance of the SQLWorkflowInstanceStore, to persist workflows when becoming idle. (It's basically derived from the example code in \ControllingWorkflowApplications from Microsoft's WCF/WF samples).

But sometimes I get an error like below:

System.Runtime.DurableInstancing.InstanceOwnerException: The execution of an InstancePersistenceCommand was interrupted because the instance owner registration for owner ID 'a426269a-be53-44e1-8580-4d0c396842e8' has become invalid. This error indicates that the in-memory copy of all instances locked by this owner have become stale and should be discarded, along with the InstanceHandles. Typically, this error is best handled by restarting the host.

I've been trying to find the cause, but it is hard to reproduce in development, on production servers however, I get it once in a while. One hint I found : when I look at the LockOwnersTable, I find the LockOnwersTable lockexpiration is set to 01/01/2000 0:0:0 and it's not getting updated anymore, while under normal circumstances the should be updated every x seconds according to the Host Lock Renewal period...

So , why whould SQLWorkflowInstanceStore stop renewing this LockExpiration and how can I detect the cause of it?

3 Answers 3

1

This happens because there are procedures running in the background and trying to extend the lock of the instance store every 30 seconds, and it seems that once the connection fail connecting to the SQL service it will mark this instance store as invalid. you can see the same behaviour if you delete the instance store record from [LockOwnersTable] table. The proposed solution is when this exception fires, you need to free the old instance store and initialize a new one

public class WorkflowInstanceStore : IWorkflowInstanceStore, IDisposable
{
    public WorkflowInstanceStore(string connectionString)
    {
        _instanceStore = new SqlWorkflowInstanceStore(connectionString);

        InstanceHandle handle = _instanceStore.CreateInstanceHandle();
        InstanceView view = _instanceStore.Execute(handle, 
            new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
        handle.Free();

        _instanceStore.DefaultInstanceOwner = view.InstanceOwner;
    }

    public InstanceStore Store
    {
        get { return _instanceStore; }
    }

    public void Dispose()
    {
        if (null != _instanceStore)
        {
            var deleteOwner = new DeleteWorkflowOwnerCommand();
            InstanceHandle handle = _instanceStore.CreateInstanceHandle();
            _instanceStore.Execute(handle, deleteOwner, TimeSpan.FromSeconds(10));
            handle.Free();
        }
    }

    private InstanceStore _instanceStore;
}

you can find the best practices to create instance store handle in this link Workflow Instance Store Best practices

1

This is an old thread but I just stumbled on the same issue.

Damir's Corner suggests to check if the instance handle is still valid before calling the instance store. I hereby quote the whole post:

Certain aspects of Workflow Foundation are still poorly documented; the persistence framework being one of them. The following snippet is typically used for setting up the instance store:

var instanceStore = new SqlWorkflowInstanceStore(connectionString);
instanceStore.HostLockRenewalPeriod = TimeSpan.FromSeconds(30);
var instanceHandle = instanceStore.CreateInstanceHandle();
var view = instanceStore.Execute(instanceHandle, 
    new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(10));
instanceStore.DefaultInstanceOwner = view.InstanceOwner;

It's difficult to find a detailed explanation of what all of this does; and to be honest, usually it's not necessary. At least not, until you start encountering problems, such as InstanceOwnerException:

The execution of an InstancePersistenceCommand was interrupted because the instance owner registration for owner ID '9938cd6d-a9cb-49ad-a492-7c087dcc93af' has become invalid. This error indicates that the in-memory copy of all instances locked by this owner have become stale and should be discarded, along with the InstanceHandles. Typically, this error is best handled by restarting the host.

The error is closely related to the HostLockRenewalPeriod property which defines how long obtained instance handle is valid without being renewed. If you try monitoring the database while an instance store with a valid instance handle is instantiated, you will notice [System.Activities.DurableInstancing].[ExtendLock] being called periodically. This stored procedure is responsible for renewing the handle. If for some reason it fails to be called within the specified HostLockRenewalPeriod, the above mentioned exception will be thrown when attempting to persist a workflow. A typical reason for this would be temporarily inaccessible database due to maintenance or networking problems. It's not something that happens often, but it's bound to happen if you have a long living instance store, e.g. in a constantly running workflow host, such as a Windows service.

Fortunately it's not all that difficult to fix the problem, once you know the cause of it. Before using the instance store you should always check, if the handle is still valid; and renew it, if it's not:

if (!instanceHandle.IsValid)
{
    instanceHandle = instanceStore.CreateInstanceHandle();
    var view = instanceStore.Execute(instanceHandle, 
        new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(10));
    instanceStore.DefaultInstanceOwner = view.InstanceOwner;
}

It's definitely less invasive than the restart of the host, suggested by the error message.

2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Mar 13, 2018 at 17:05
  • Rosario, You are absolutely right. I edited my answer quoting the whole post. Mar 13, 2018 at 17:51
0

you have to be sure about expiration of owner user

here how I am used to handle this issue

     public SqlWorkflowInstanceStore SetupSqlpersistenceStore()
    {
        SqlWorkflowInstanceStore sqlWFInstanceStore = new SqlWorkflowInstanceStore(ConfigurationManager.ConnectionStrings["DB_WWFConnectionString"].ConnectionString);
        sqlWFInstanceStore.InstanceCompletionAction = InstanceCompletionAction.DeleteAll;
        InstanceHandle handle = sqlWFInstanceStore.CreateInstanceHandle();
        InstanceView view = sqlWFInstanceStore.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
        handle.Free();
        sqlWFInstanceStore.DefaultInstanceOwner = view.InstanceOwner;
        return sqlWFInstanceStore;
    }

and here how you can use this method

     wfApp.InstanceStore = SetupSqlpersistenceStore();

wish this help

5
  • Just try the sample ControllingWorkflowApplications from Microsoft WF samples in the folder Basic. Start a Parallel1.xaml (included in sample). Then stop sql server service for at least 30 seconds. Restart sql server and try to resume a bookmark, it will abort the workflow...
    – rekna
    May 28, 2013 at 19:35
  • @rekna - we're experiencing the same issue - did you ever find a resolution? Sep 13, 2013 at 21:06
  • 1
    Unfortunately not... I have a case open with MS but at this moment, I don not receivee a response from them. I'm using a workaround (which is for from perfect though), which monitors the workflow database to find out if the lockexpiration is still valid, if not, restart workflow instances. I really don't understand the logic behind it. I wish I could write a custom instancestore, but MS made it too complicated. It's practically impossible to derive from the existing instance store.
    – rekna
    Sep 14, 2013 at 8:18
  • Thanks @rekna. Note that this article mentions an example XML instancestore (from MS example) and an example in-memory instancestore. Maybe you can glean some help from these. social.msdn.microsoft.com/Forums/en-US/… Sep 17, 2013 at 14:23
  • I'm a bit reluctant to write my own completely, when upgrading to newer versions of WF things might change (as from 4.0 to 4.5), it's a risk. Better would be, if instancestore was easily extendable, so only the desired behavior can be changed... unfortunately that is not the case
    – rekna
    Sep 19, 2013 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.