Skip to content

Commit

Permalink
fix it so that disposing the RocksDb instance twice does not throw Ac…
Browse files Browse the repository at this point in the history
…cessViolationException and crash the process
  • Loading branch information
mookid8000 committed May 11, 2020
1 parent 5b4b0bc commit 7727d24
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
19 changes: 15 additions & 4 deletions RocksDbSharp/RocksDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace RocksDbSharp
{
public class RocksDb : IDisposable
{
bool disposed;

internal static ReadOptions DefaultReadOptions { get; } = new ReadOptions();
internal static OptionsHandle DefaultOptions { get; } = new DbOptions();
internal static WriteOptions DefaultWriteOptions { get; } = new WriteOptions();
Expand All @@ -31,12 +33,21 @@ private RocksDb(IntPtr handle, dynamic optionsReferences, dynamic cfOptionsRefs,

public void Dispose()
{
if (columnFamilies != null)
if (disposed) return;
try
{
if (columnFamilies != null)
{
foreach (var cfh in columnFamilies.Values)
cfh.Dispose();
}

Native.Instance.rocksdb_close(Handle);
}
finally
{
foreach (var cfh in columnFamilies.Values)
cfh.Dispose();
disposed = true;
}
Native.Instance.rocksdb_close(Handle);
}

public static RocksDb Open(OptionsHandle options, string path)
Expand Down
44 changes: 44 additions & 0 deletions tests/RocksDbSharpTest/LifestyleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.IO;
using RocksDbSharp;
using Xunit;
// ReSharper disable RedundantArgumentDefaultValue
// ReSharper disable ArgumentsStyleLiteral

namespace RocksDbSharpTest
{
public class LifestyleTest
{
[Fact]
public void DoubleDisposableDoesNotThrow()
{
var testdir = Path.Combine(Path.GetTempPath(), "lifestyle_test");
var testdb = Path.Combine(testdir, "main");
var path = Environment.ExpandEnvironmentVariables(testdb);

if (Directory.Exists(testdir))
{
Directory.Delete(testdir, recursive: true);
}

Directory.CreateDirectory(testdir);

var options = new DbOptions().SetCreateIfMissing(true).EnableStatistics();

var db = RocksDb.Open(options, path);

db.Dispose();

// throws AccessViolationException, which on my machine crashed the process so hard that XUnit coulnd't cope...
//
db.Dispose();
//
// got this in Event Viewer though:
//
// Application: dotnet.exe
// CoreCLR Version: 4.6.28619.1
// Description: The process was terminated due to an internal error in the .NET Runtime at IP 00007FFF39BC5AA3 (00007FFF39A20000) with exit code c0000005.
//
}
}
}

0 comments on commit 7727d24

Please sign in to comment.