Skip to content

Fix the transfer buffer floor: 2 ^ 23 is XOR, not 8MB - #17

Open
totoantibes wants to merge 1 commit into
dougforpres:workerThreadfrom
totoantibes:fix/transfer-buffer-size
Open

totoantibes wants to merge 1 commit into
dougforpres:workerThreadfrom
totoantibes:fix/transfer-buffer-size

Conversation

@totoantibes

Copy link
Copy Markdown

The read path sizes its transfer buffer like this:

// Use up to 8MB buffer, currently the optimal buffer size seems to be 256kB
ULONG bufferSize = maximumMemoryMode ? cbReportedDataSize
                 : min(cbReportedDataSize, max(cbOptimalDataSize * 16, 2 ^ 23));

^ is XOR in C++, not exponentiation, so 2 ^ 23 is 21. The intended 8 MB floor is a twenty-one byte floor.

Why it has not bitten

cbOptimalDataSize * 16 is a few MB in normal operation and wins the max(), so the broken floor is never reached and the buffer ends up around 4 MB instead of the intended 8 MB. Nothing visibly breaks.

Why it can

cbOptimalDataSize is initialised to 0 and filled by the query immediately above:

ULONG cbReportedDataSize = 0;
ULONG cbOptimalDataSize = 0;

hr = spResults->GetUnsignedIntegerValue(WPD_PROPERTY_MTP_EXT_OPTIMAL_TRANSFER_BUFFER_SIZE,
    &cbOptimalDataSize);

hr = spResults->GetUnsignedIntegerValue(WPD_PROPERTY_MTP_EXT_TRANSFER_TOTAL_DATA_SIZE,
    &cbReportedDataSize);

The first hr is overwritten by the second call before anything tests it, so a device that does not answer OPTIMAL_TRANSFER_BUFFER_SIZE leaves the variable at 0 and the failure is silent. max(0, 21) is then the entire answer, and the read loop transfers 21 bytes per round trip — about 600,000 READ_DATA commands for a 12 MB image, which in practice is a hang rather than a slow download.

I have not observed this in the wild, so this is a latent bug rather than a reported one. It is worth fixing because the guard that makes it unreachable is the accident, not the design.

The change

Replace the literal with a named constant:

static const ULONG TRANSFER_BUFFER_TARGET = 8 * 1024 * 1024;

ULONG bufferSize = maximumMemoryMode ? cbReportedDataSize
                 : min(cbReportedDataSize, max(cbOptimalDataSize * 16, TRANSFER_BUFFER_TARGET));

That does two things at once: it restores the 8 MB the comment describes, and it makes a missing optimal size harmless, since the floor is now a sane buffer rather than 21 bytes.

Note this does change behaviour in the normal path — the buffer becomes 8 MB where it was ~4 MB. That is what the comment intends, and it is a transient allocation freed at the end of the same block, but it is a real change rather than a pure no-op and worth being aware of when merging.

Built clean (x64/Release, driver 1.0.1.21). Based on workerThread to match #16.

The read path sizes its transfer buffer with

    min(cbReportedDataSize, max(cbOptimalDataSize * 16, 2 ^ 23))

intending an 8MB floor, as the comment two lines above says. But `^` is XOR in
C++, not exponentiation, so `2 ^ 23` is 21 - the floor is twenty-one BYTES.

This is normally invisible, because cbOptimalDataSize * 16 is a few MB and wins
the max(). It only surfaces when cbOptimalDataSize is 0, and that is reachable:
the variable is initialised to 0 and filled by the
OPTIMAL_TRANSFER_BUFFER_SIZE query whose hr is immediately overwritten by the
TOTAL_DATA_SIZE query on the next line, so a device that does not answer it
fails silently. The floor is then the whole answer, and a 21-byte buffer turns
a 12MB image into roughly 600,000 READ_DATA round trips.

Replace the literal with a named 8MB constant, which both restores the intended
size and makes a missing optimal size harmless.
@shurkanTwo

shurkanTwo commented Oct 17, 2026 •

Copy link
Copy Markdown

Hi @totoantibes, thank you for this fix. Your original commit ac9bf1feb9ed6d24f587176905d7c7818ae64787 is merged intact into SonyCamera native 1.0.1.22 in my maintained fork. Original commit authorship and contributor attribution are preserved, with @dougforpres credited for the original project.

The release provides x86/x64 native DLL and diagnostic-tool packages, complete matching native/dependency source, license notices and SHA256SUMS.txt. It is marked regular/Latest on GitHub; the original 1.0.1.22-preview.1 tag and filenames are retained to preserve existing links.

Manual installation and rollback instructions explain choosing the matching architecture, confirming the client's loaded DLL paths and backing up all three native files. These packages contain the native components and diagnostic tool; no ASCOM wrapper build or installer is included.

Windows CI passed both architectures, DLL/export/dependency checks and seven buffer-calculation cases, including rejection of the old calculation. The complete source ZIP also rebuilds on Windows outside Git. Physical camera captures, WPD/USB operation and ASCOM/NINA integration remain unverified for this combined build.

Device-removal PR #16 is not included in 1.0.1.22. Its original contribution and follow-up fixes are tracked separately in draft PR #3 in my fork, pending physical-camera validation. This release does not claim to fix USB-removal crashes.

Please report results or problems with the maintained packages in the fork's issue tracker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants