Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions Sally7/S7Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,10 @@ await stream
public async Task ReadAsync(IDataItem[] dataItems, CancellationToken cancellationToken = default)
{
var results = ArrayPool<ReadWriteErrorCode>.Shared.Rent(dataItems.Length);
try
{
await ReadAsync(dataItems, results, cancellationToken).ConfigureAwait(false);
await ReadAsync(dataItems, results, cancellationToken).ConfigureAwait(false);

ReadWriteErrorHelpers.ThrowIfHasErrors("Read", dataItems, results.AsSpan(0, dataItems.Length));
}
finally
{
ArrayPool<ReadWriteErrorCode>.Shared.Return(results);
}
ReadWriteErrorHelpers.ThrowIfHasErrors("Read", dataItems, results.AsSpan(0, dataItems.Length));
ArrayPool<ReadWriteErrorCode>.Shared.Return(results);
Comment on lines 229 to +233
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArrayPool<ReadWriteErrorCode>.Shared.Rent(...) is no longer paired with a Return(...) on exception paths (including the common path where ThrowIfHasErrors throws for per-item failures). This defeats pooling under failure conditions and can cause sustained allocations/GC pressure (and potentially LOH allocations for large dataItems.Length) when a PLC is offline or returns item errors repeatedly.

Consider restoring a try/finally that always returns the buffer, or if the intent is specifically to avoid returning on transport/cancellation exceptions, then catch only the expected AggregateException from ThrowIfHasErrors (return the buffer, then rethrow) while still letting other exceptions skip return if you really need that behavior.

Copilot uses AI. Check for mistakes.
}

/// <summary>
Expand Down Expand Up @@ -297,16 +291,10 @@ public async Task ReadAsync(IDataItem[] dataItems, Memory<ReadWriteErrorCode> re
public async Task WriteAsync(IDataItem[] dataItems, CancellationToken cancellationToken = default)
{
var results = ArrayPool<ReadWriteErrorCode>.Shared.Rent(dataItems.Length);
try
{
await WriteAsync(dataItems, results, cancellationToken).ConfigureAwait(false);
await WriteAsync(dataItems, results, cancellationToken).ConfigureAwait(false);

ReadWriteErrorHelpers.ThrowIfHasErrors("Write", dataItems, results.AsSpan(0, dataItems.Length));
}
finally
{
ArrayPool<ReadWriteErrorCode>.Shared.Return(results);
}
ReadWriteErrorHelpers.ThrowIfHasErrors("Write", dataItems, results.AsSpan(0, dataItems.Length));
ArrayPool<ReadWriteErrorCode>.Shared.Return(results);
Comment on lines 293 to +297
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same pooling issue as in ReadAsync: if WriteAsync(...) throws (including ThrowIfHasErrors throwing AggregateException for per-item failures), the rented ArrayPool buffer is never returned. This can significantly increase allocation rate and memory pressure in repeated error scenarios.

Recommendation: ensure the rented buffer is returned for at least the expected error case (AggregateException from ThrowIfHasErrors) and ideally via try/finally to keep pooling effective and consistent with the existing ArrayPool<byte> usage earlier in this file.

Copilot uses AI. Check for mistakes.
}

/// <summary>
Expand Down
Loading