Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hangfire InMemory 0.9.0 Object disposed exception #2399

Open
Cynicszm opened this issue Apr 29, 2024 · 3 comments
Open

Hangfire InMemory 0.9.0 Object disposed exception #2399

Cynicszm opened this issue Apr 29, 2024 · 3 comments

Comments

@Cynicszm
Copy link

Cynicszm commented Apr 29, 2024

Hi,

We are now getting object disposed exceptions in NUnit (4.1.0) after upgrading Hangfire InMemory from 0.8.1 to 0.9.0

Individual tests pass but fail on committing the write transaction when run concurrently.

Code snippet:
`public string CreateToken( TimeSpan expireIn )
{
var tokenId = Guid.NewGuid().ToString( "N" );
var resourceKey = "execution-tokens:" + tokenId;

using var connection = JobStorage.Current.GetConnection();
using var transaction = connection.CreateWriteTransaction();
transaction.SetRangeInHash( resourceKey, new Dictionary<string, string>
{
    { "Cancelled", "false" },
    { "Paused", "false" }
} );

// if storage supports manual expiration handling
if ( transaction is JobStorageTransaction jsTransaction )
{
    jsTransaction.ExpireHash( resourceKey, expireIn );
}
transaction.Commit();

return tokenId;

}`

Stack:
System.ObjectDisposedException : Cannot access a disposed object.
Object name: 'Hangfire.InMemory.InMemoryDispatcher'.
at Hangfire.InMemory.InMemoryDispatcher.ThrowObjectDisposedException() in //src/Hangfire.InMemory/InMemoryDispatcher.cs:line 136
at Hangfire.InMemory.InMemoryDispatcher.QueryAndWait(Func3 query) in /_/src/Hangfire.InMemory/InMemoryDispatcher.cs:line 58 at Hangfire.InMemory.InMemoryDispatcherBase.QueryAndWait[T](Func3 query) in /
/src/Hangfire.InMemory/InMemoryDispatcherBase.cs:line 126
at Hangfire.InMemory.InMemoryDispatcherBase.QueryAndWait(Action`2 query) in //src/Hangfire.InMemory/InMemoryDispatcherBase.cs:line 120
at Hangfire.InMemory.InMemoryTransaction.Commit() in /
/src/Hangfire.InMemory/InMemoryTransaction.cs:line 51

@Cynicszm Cynicszm changed the title Hangfire InMemory 1.9.0 Object disposed exception Hangfire InMemory 0.9.0 Object disposed exception Apr 29, 2024
@odinserj
Copy link
Member

InMemoryStorage class now implements IDisposable. So something is calling the Dispose method. Try creating instances of this class explicitly for each test, instead of relying on a static instance in JobStorage.Current. In this case, unit tests will be properly isolated, and you will not have any shared state between test runs.

@Cynicszm
Copy link
Author

Thanks for the quick response.

I've moved some of the JobStorage usages around and it appears to be behaving for now.

Appreciate the help.

@odinserj
Copy link
Member

Great, thanks for the update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants