Skip to content

Commit c9bbca0

Browse files
paulirwinclaude
andcommitted
Fix DatasetSplitter cancellation exception handling
- Add catch for OperationCanceledException before the generic catch so cancellation isn't wrapped in IOException - Replace ThrowIfCancellationRequested in finally with a conditional check to avoid masking exceptions during unwinding - Fix param doc to say "split operation" instead of "search" Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1110579 commit c9bbca0

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

src/Lucene.Net.Classification/Utils/DatasetSplitter.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public virtual void Split(AtomicReader originalIndex,
7676
/// <param name="testIndex">a <see cref="Directory"/> used to write the test index</param>
7777
/// <param name="crossValidationIndex">a <see cref="Directory"/> used to write the cross validation index</param>
7878
/// <param name="analyzer"><see cref="Analyzer"/> used to create the new docs</param>
79-
/// <param name="cancellationToken">A cancellation token to cancel the search.</param>
79+
/// <param name="cancellationToken">A cancellation token to cancel the split operation.</param>
8080
/// <param name="fieldNames">names of fields that need to be put in the new indexes or <c>null</c> if all should be used</param>
8181
/// <exception cref="IOException">if any writing operation fails on any of the indexes</exception>
8282
/// <exception cref="OperationCanceledException">if the <paramref name="cancellationToken"/> requested cancellation</exception>
@@ -164,20 +164,22 @@ public virtual void Split(AtomicReader originalIndex,
164164
b++;
165165
}
166166
}
167+
catch (OperationCanceledException) { throw; } // LUCENENET: Don't wrap cancellation in IOException
167168
catch (Exception e) when (e.IsException())
168169
{
169170
throw new IOException("Exception in DatasetSplitter", e);
170171
}
171172
finally
172173
{
173-
// LUCENENET Specific - if we've gotten this far and cancellation was requested, don't commit
174-
cancellationToken.ThrowIfCancellationRequested();
175-
176-
testWriter.Commit();
177-
cvWriter.Commit();
178-
trainingWriter.Commit();
174+
// LUCENENET Specific - if cancellation was requested, don't commit.
175+
if (!cancellationToken.IsCancellationRequested)
176+
{
177+
testWriter.Commit();
178+
cvWriter.Commit();
179+
trainingWriter.Commit();
180+
}
179181

180-
// close IWs - LUCENENET specific removal: disposed via `using` declaration
182+
// close IWs - LUCENENET specific: disposed via `using` declaration
181183
// testWriter.Dispose();
182184
// cvWriter.Dispose();
183185
// trainingWriter.Dispose();

0 commit comments

Comments
 (0)