Skip to content

Commit 3f7b4e1

Browse files
ToChannel(IDbCommand, ..., transform) closes a connection it opened when the provider fails to execute
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent f8d6b77 commit 3f7b4e1

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

‎Source/Channel/ToChannel.Target.cs‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private static async ValueTask<long> ToChannelCore<T>(
9595
/// <summary>
9696
/// Iterates an <see cref="IDataReader"/> and writes each record as an array of values to the target channel.
9797
/// </summary>
98-
/// <param name="reader">The data reader to iterate. The caller owns it: it is not closed or disposed here, and it must stay open until the operation completes.</param>
98+
/// <param name="reader">The data reader to iterate. The caller owns it: it is not closed or disposed here, and it must stay open until the operation completes. It is moved on to the next result set once the last row has been read, so that an error that follows the rows is raised.</param>
9999
/// <param name="target">The channel writer that receives the records.</param>
100100
/// <param name="complete">If <see langword="true"/>, the target is completed once all the records have been written to it (or the source is empty).</param>
101101
/// <param name="cancellationToken">A token that can cancel the operation.</param>
@@ -115,7 +115,7 @@ public static ValueTask<long> ToChannel(this IDataReader reader,
115115
/// <summary>
116116
/// Iterates an <see cref="IDataReader"/> and writes each record as an array of values rented from an array pool to the target channel.
117117
/// </summary>
118-
/// <param name="reader">The data reader to iterate. The caller owns it: it is not closed or disposed here, and it must stay open until the operation completes.</param>
118+
/// <param name="reader">The data reader to iterate. The caller owns it: it is not closed or disposed here, and it must stay open until the operation completes. It is moved on to the next result set once the last row has been read, so that an error that follows the rows is raised.</param>
119119
/// <param name="target">The channel writer that receives the records.</param>
120120
/// <param name="complete">If <see langword="true"/>, the target is completed once all the records have been written to it (or the source is empty).</param>
121121
/// <param name="arrayPool">The array pool to rent each record's array from, or <see langword="null"/> to allocate new arrays. The consumer of the channel is responsible for returning each array to the pool.</param>
@@ -138,7 +138,7 @@ public static ValueTask<long> ToChannel(this IDataReader reader,
138138
/// Iterates an <see cref="IDataReader"/> and writes each record after it has been transformed by <paramref name="transform"/> to the target channel.
139139
/// </summary>
140140
/// <typeparam name="T">The type of the items written to the channel: the return type of the transform function.</typeparam>
141-
/// <param name="reader">The data reader to iterate. The caller owns it: it is not closed or disposed here, and it must stay open until the operation completes.</param>
141+
/// <param name="reader">The data reader to iterate. The caller owns it: it is not closed or disposed here, and it must stay open until the operation completes. It is moved on to the next result set once the last row has been read, so that an error that follows the rows is raised.</param>
142142
/// <param name="target">The channel writer that receives the records.</param>
143143
/// <param name="complete">If <see langword="true"/>, the target is completed once all the records have been written to it (or the source is empty).</param>
144144
/// <param name="transform">The function that transforms each <see cref="IDataRecord"/> into the item written to the channel.</param>
@@ -161,7 +161,7 @@ public static ValueTask<long> ToChannel<T>(this IDataReader reader,
161161
/// Iterates an <see cref="IDataReader"/> and writes each record mapped to a new instance of <typeparamref name="T"/> to the target channel.
162162
/// </summary>
163163
/// <typeparam name="T">The type each record is mapped to.</typeparam>
164-
/// <param name="reader">The data reader to iterate. The caller owns it: it is not closed or disposed here, and it must stay open until the operation completes.</param>
164+
/// <param name="reader">The data reader to iterate. The caller owns it: it is not closed or disposed here, and it must stay open until the operation completes. It is moved on to the next result set once the last row has been read, so that an error that follows the rows is raised.</param>
165165
/// <param name="target">The channel writer that receives the records.</param>
166166
/// <param name="complete">If <see langword="true"/>, the target is completed after the last record has been delivered to it, or with the error that ended the operation.</param>
167167
/// <param name="cancellationToken">A token that can cancel the operation.</param>
@@ -181,7 +181,7 @@ public static ValueTask<long> ToChannel<T>(this IDataReader reader,
181181
/// </summary>
182182
/// <remarks>Declare the <see cref="RowMap{T}"/> once and pass it: a map built in the call (a <c>new RowMap&lt;T&gt;(...)</c> or a collection expression in the argument) is rebuilt, and its row reader compiled, on every call.</remarks>
183183
/// <typeparam name="T">The type each record is mapped to.</typeparam>
184-
/// <param name="reader">The data reader to iterate. The caller owns it: it is not closed or disposed here, and it must stay open until the operation completes.</param>
184+
/// <param name="reader">The data reader to iterate. The caller owns it: it is not closed or disposed here, and it must stay open until the operation completes. It is moved on to the next result set once the last row has been read, so that an error that follows the rows is raised.</param>
185185
/// <param name="target">The channel writer that receives the records.</param>
186186
/// <param name="complete">If <see langword="true"/>, the target is completed after the last record has been delivered to it, or with the error that ended the operation.</param>
187187
/// <param name="map">An optional prebuilt <see cref="RowMap{T}"/>; <see langword="null"/> reads by column name.</param>
@@ -291,8 +291,20 @@ public static async ValueTask<long> ToChannel<T>(this IDbCommand command,
291291
ConnectionState state = command.Connection.EnsureOpen();
292292
CommandBehavior behavior = CommandBehavior.Default;
293293
if (state == ConnectionState.Closed) behavior |= CommandBehavior.CloseConnection;
294-
using IDataReader reader = await command.ExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
295-
return await reader.ToChannel(target, false, transform, cancellationToken).ConfigureAwait(false);
294+
IDataReader reader;
295+
try
296+
{
297+
reader = await command.ExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
298+
}
299+
catch
300+
{
301+
// No reader means CloseConnection has nothing to close: the connection this call opened is closed here (never disposed).
302+
if (state == ConnectionState.Closed) CommandExtensions.CloseOpened(command);
303+
throw;
304+
}
305+
306+
using (reader)
307+
return await reader.ToChannel(target, false, transform, cancellationToken).ConfigureAwait(false);
296308
}
297309
catch (Exception ex)
298310
{

‎Source/Core/Extensions/Command._.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static async ValueTask<DbDataReader> OpenAndExecuteReaderAsync(this DbCommand co
7979
}
8080
}
8181

82-
static void CloseOpened(IDbCommand command)
82+
internal static void CloseOpened(IDbCommand command)
8383
{
8484
try
8585
{

0 commit comments

Comments
 (0)