|
3 | 3 | using Lucene.Net.Index.Extensions; |
4 | 4 | using NUnit.Framework; |
5 | 5 | using System; |
| 6 | +using System.Diagnostics; |
6 | 7 | using System.IO; |
7 | 8 | using System.Text; |
| 9 | +using System.Threading; |
8 | 10 | using Assert = Lucene.Net.TestFramework.Assert; |
9 | 11 |
|
10 | 12 | namespace Lucene.Net.Store |
@@ -415,6 +417,115 @@ private void AssertChunking(Random random, int chunkSize) |
415 | 417 | } |
416 | 418 |
|
417 | 419 |
|
| 420 | + // LUCENENET: Regression test for GitHub #1090. A background thread |
| 421 | + // extends a file on disk while the foreground thread repeatedly |
| 422 | + // opens it with MMapDirectory.OpenInput. Before the fix, the |
| 423 | + // file's length could grow between the caller capturing fc.Length |
| 424 | + // and MemoryMappedFile.CreateFromFile performing its internal |
| 425 | + // stat, causing ArgumentOutOfRangeException (paramName="capacity") |
| 426 | + // with the message "The capacity may not be smaller than the |
| 427 | + // file size." |
| 428 | + [Test, LuceneNetSpecific, Slow] |
| 429 | + public void TestOpenInputConcurrentFileExtension_Issue1090() |
| 430 | + { |
| 431 | + var dir = CreateTempDir("testOpenInputConcurrentFileExtension"); |
| 432 | + const string name = "data.bin"; |
| 433 | + string filePath = Path.Combine(dir.FullName, name); |
| 434 | + |
| 435 | + // Seed with a small initial payload. |
| 436 | + File.WriteAllBytes(filePath, new byte[64]); |
| 437 | + |
| 438 | + using var mmapDir = new MMapDirectory(dir); |
| 439 | + |
| 440 | + const long maxFileSize = 1L * 1024 * 1024; // 1 MiB cap |
| 441 | + var stop = new ManualResetEventSlim(false); |
| 442 | + Exception writerError = null; |
| 443 | + |
| 444 | + var writer = new Thread(() => |
| 445 | + { |
| 446 | + var chunk = new byte[64]; |
| 447 | + try |
| 448 | + { |
| 449 | + while (!stop.IsSet) |
| 450 | + { |
| 451 | + using var fs = new FileStream(filePath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite); |
| 452 | + fs.Seek(0, SeekOrigin.End); |
| 453 | + if (fs.Length < maxFileSize) |
| 454 | + { |
| 455 | + fs.Write(chunk, 0, chunk.Length); |
| 456 | + } |
| 457 | + else |
| 458 | + { |
| 459 | + // Keep the file bounded: truncate back and grow again. |
| 460 | + fs.SetLength(64); |
| 461 | + } |
| 462 | + } |
| 463 | + } |
| 464 | + catch (Exception e) |
| 465 | + { |
| 466 | + writerError = e; |
| 467 | + } |
| 468 | + }) |
| 469 | + { IsBackground = true, Name = "mmap-issue1090-extender" }; |
| 470 | + writer.Start(); |
| 471 | + |
| 472 | + // Snapshot counters so this test's assertion is not affected by |
| 473 | + // any earlier test's activity on MMapDirectory. |
| 474 | + long baselineRetries = Interlocked.Read(ref MMapDirectory.s_capacityRetryCount); |
| 475 | + |
| 476 | + try |
| 477 | + { |
| 478 | + var sw = Stopwatch.StartNew(); |
| 479 | + int iterations = 0; |
| 480 | + // Keep stretching the window until either the race fires or we |
| 481 | + // hit a hard deadline. On most machines this takes < 1 second. |
| 482 | + const int maxSeconds = 15; |
| 483 | + while (sw.Elapsed < TimeSpan.FromSeconds(maxSeconds)) |
| 484 | + { |
| 485 | + using (var _ = mmapDir.OpenInput(name, NewIOContext(Random))) |
| 486 | + { |
| 487 | + // Just open and dispose; the race occurs during construction. |
| 488 | + } |
| 489 | + iterations++; |
| 490 | + |
| 491 | + if (Interlocked.Read(ref MMapDirectory.s_capacityRetryCount) > baselineRetries) |
| 492 | + { |
| 493 | + break; // race reproduced and handled by the retry loop |
| 494 | + } |
| 495 | + } |
| 496 | + |
| 497 | + long retries = Interlocked.Read(ref MMapDirectory.s_capacityRetryCount) - baselineRetries; |
| 498 | + int maxAttempts = Volatile.Read(ref MMapDirectory.s_maxCapacityAttemptsObserved); |
| 499 | + |
| 500 | + // Surface what was observed for diagnostics when run with -v normal. |
| 501 | + TestContext.Progress.WriteLine( |
| 502 | + $"TestOpenInputConcurrentFileExtension: iterations={iterations}, retries={retries}, maxAttemptsObserved={maxAttempts}"); |
| 503 | + |
| 504 | + // The real check: the race must have fired and our retry loop |
| 505 | + // must have swallowed it. Without the fix, the exception |
| 506 | + // escapes OpenInput and the test fails with ArgumentOutOfRangeException |
| 507 | + // (as seen in #1090). If the race never fires during this run |
| 508 | + // (timing-dependent), mark the test inconclusive rather than |
| 509 | + // silently passing — we haven't actually exercised the fix. |
| 510 | + if (retries == 0) |
| 511 | + { |
| 512 | + NUnit.Framework.Assert.Inconclusive( |
| 513 | + $"The concurrent-extension race was not reproduced within {maxSeconds}s " + |
| 514 | + $"({iterations} OpenInput iterations). The fix was therefore not exercised on this run."); |
| 515 | + } |
| 516 | + } |
| 517 | + finally |
| 518 | + { |
| 519 | + stop.Set(); |
| 520 | + writer.Join(); |
| 521 | + } |
| 522 | + |
| 523 | + if (writerError != null) |
| 524 | + { |
| 525 | + throw new Exception("Writer thread failed", writerError); |
| 526 | + } |
| 527 | + } |
| 528 | + |
418 | 529 | [Test, LuceneNetSpecific] |
419 | 530 | public void TestDisposeIndexInput() |
420 | 531 | { |
|
0 commit comments