Skip to content

Commit 760774d

Browse files
committed
Multiple disposing fixes
1 parent 2cc00c7 commit 760774d

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

LiteDB/Engine/Disk/DiskService.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ public DiskService(
5555
{
5656
LOG($"creating new database: '{Path.GetFileName(_dataFactory.Name)}'", "DISK");
5757

58-
this.Initialize(_dataPool.Writer.Value, settings.Collation, settings.InitialSize);
58+
try
59+
{
60+
this.Initialize(_dataPool.Writer.Value, settings.Collation, settings.InitialSize);
61+
}
62+
catch (Exception ex)
63+
{
64+
LOG($"Error while initializing DiskService: {ex.Message}", "ERROR");
65+
throw;
66+
}
5967
}
6068

6169
// if not readonly, force open writable datafile

LiteDB/Engine/Services/RebuildService.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ public long Rebuild(RebuildOptions options)
5151
// open file reader and ready to import to new temp engine instance
5252
reader.Open();
5353

54-
// open new engine to recive all data readed from FileReader
55-
using (var engine = new LiteEngine(new EngineSettings
56-
{
57-
Filename = tempFilename,
58-
Collation = options.Collation,
59-
Password = options.Password,
60-
}))
54+
try
6155
{
56+
// open new engine to recive all data readed from FileReader
57+
using var engine = new LiteEngine(new EngineSettings
58+
{
59+
Filename = tempFilename,
60+
Collation = options.Collation,
61+
Password = options.Password,
62+
});
6263
// copy all database to new Log file with NO checkpoint during all rebuild
6364
engine.Pragma(Pragmas.CHECKPOINT, 0);
6465

@@ -85,6 +86,11 @@ public long Rebuild(RebuildOptions options)
8586
// after rebuild, copy log bytes into data file
8687
engine.Checkpoint();
8788
}
89+
catch (Exception)
90+
{
91+
File.Delete(tempFilename);
92+
throw;
93+
}
8894
}
8995

9096
// if log file exists, rename as backup file

LiteDB/Engine/Services/TransactionService.cs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -398,32 +398,40 @@ protected virtual void Dispose(bool dispose)
398398
return;
399399
}
400400

401-
ENSURE(_state != TransactionState.Disposed, "transaction must be active before call Done");
402-
403-
// clean snapshots if there is no commit/rollback
404-
if (_state == TransactionState.Active && _snapshots.Count > 0)
401+
try
405402
{
406-
// release writable snapshots
407-
foreach (var snapshot in _snapshots.Values.Where(x => x.Mode == LockMode.Write))
408-
{
409-
// discard all dirty pages
410-
_disk.DiscardDirtyPages(snapshot.GetWritablePages(true, true).Select(x => x.Buffer));
411403

412-
// discard all clean pages
413-
_disk.DiscardCleanPages(snapshot.GetWritablePages(false, true).Select(x => x.Buffer));
414-
}
404+
ENSURE(_state != TransactionState.Disposed, "transaction must be active before call Done");
415405

416-
// release buffers in read-only snaphosts
417-
foreach (var snapshot in _snapshots.Values.Where(x => x.Mode == LockMode.Read))
406+
// clean snapshots if there is no commit/rollback
407+
if (_state == TransactionState.Active && _snapshots.Count > 0)
418408
{
419-
foreach (var page in snapshot.LocalPages)
409+
// release writable snapshots
410+
foreach (var snapshot in _snapshots.Values.Where(x => x.Mode == LockMode.Write))
420411
{
421-
page.Buffer.Release();
412+
// discard all dirty pages
413+
_disk.DiscardDirtyPages(snapshot.GetWritablePages(true, true).Select(x => x.Buffer));
414+
415+
// discard all clean pages
416+
_disk.DiscardCleanPages(snapshot.GetWritablePages(false, true).Select(x => x.Buffer));
422417
}
423418

424-
snapshot.CollectionPage?.Buffer.Release();
419+
// release buffers in read-only snaphosts
420+
foreach (var snapshot in _snapshots.Values.Where(x => x.Mode == LockMode.Read))
421+
{
422+
foreach (var page in snapshot.LocalPages)
423+
{
424+
page.Buffer.Release();
425+
}
426+
427+
snapshot.CollectionPage?.Buffer.Release();
428+
}
425429
}
426430
}
431+
catch (Exception ex)
432+
{
433+
LOG($"Error while disposing TransactionService: {ex.Message}", "ERROR");
434+
}
427435

428436
_reader.Dispose();
429437

0 commit comments

Comments
 (0)