Skip to content

Commit

Permalink
Fix co-variant array conversion breaking shared ArrayPool (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
akpl authored Aug 27, 2024
1 parent 7c95372 commit fa8fc72
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions ClickHouse.Client.Tests/BulkCopyTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -430,6 +431,29 @@ public async Task ShouldThrowExceptionOnInnerException(double fraction)
Assert.ThrowsAsync<ClickHouseBulkCopySerializationException>(async () => await bulkCopy.WriteToServerAsync(data, CancellationToken.None));
}

[Test]
public async Task ShouldNotAffectSharedArrayPool()
{
var targetTable = "test." + SanitizeTableName($"array_pool");

await connection.ExecuteStatementAsync($"DROP TABLE IF EXISTS {targetTable}");
await connection.ExecuteStatementAsync($"CREATE TABLE IF NOT EXISTS {targetTable} (int Int32, str String, dt DateTime) ENGINE Null");

const int poolSize = 8;
using var bulkCopy = new ClickHouseBulkCopy(connection)
{
DestinationTableName = targetTable,
BatchSize = poolSize
};

await bulkCopy.InitAsync();
await bulkCopy.WriteToServerAsync(Enumerable.Repeat(new object[] { 0, "a", DateTime.Now }, 100));

var rentedArray = ArrayPool<object>.Shared.Rent(poolSize);
Assert.DoesNotThrow(() => { rentedArray[0] = 1; });
ArrayPool<object>.Shared.Return(rentedArray);
}

private static string SanitizeTableName(string input)
{
var builder = new StringBuilder();
Expand Down
4 changes: 2 additions & 2 deletions ClickHouse.Client/Copy/ClickHouseBulkCopy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private IEnumerable<Batch> IntoBatches(IEnumerable<object[]> rows, string query,
// Convenience argument collection
private struct Batch : IDisposable
{
public object[] Rows;
public object[][] Rows;
public int Size;
public string Query;
public ClickHouseType[] Types;
Expand All @@ -227,7 +227,7 @@ public void Dispose()
{
if (Rows != null)
{
ArrayPool<object>.Shared.Return(Rows);
ArrayPool<object[]>.Shared.Return(Rows);
Rows = null;
}
}
Expand Down

0 comments on commit fa8fc72

Please sign in to comment.