Skip to content

[Bug] Multi-file program arrives corrupted over BLE (SPIKE Prime, v4.1.0 / v4.1.1, Windows); USB and small BLE writes are fine #2871

Open

Description

@nablejames31-beep

Summary
Uploading a multi-file program (~45–52 KB, 9 modules) to a SPIKE Prime hub over Bluetooth corrupts a few bytes of the program data. The program then fails while loading an imported module. The same program uploaded over USB with Pybricks Code runs correctly. When the upload is done in BLE writes of 200 bytes instead of the default size, it also arrives intact.

Environment
Hub: SPIKE Prime (primehub)
Firmware 4.1.1 (ci-release-113-v4.1.1 on 2026-10-22).
The same problem on 4.1.0 (ci-release-112-v4.1.0 on 2026-10-12).
PC: Windows 11 Pro Education 10.0.26200, Bluetooth adapter MediaTek MT7921. The negotiated BLE MTU is 515, as reported by bleak.
Clients that show it: Pybricks Code in Brave (Web Bluetooth), and pybricksdev 2.3.2 (bleak / WinRT).
Not affected: Pybricks Code over USB.
Symptoms
The errors are raised while an imported module is being loaded. The traceback points at the import line in the main script.

ValueError: invalid syntax for number (most common; it repeats every time for the same program).
MemoryError: memory allocation failed, allocating 1841332 bytes.
ImportError: no module named 'pybricksk\x00\x1cols'. The module imported pybricks.tools.
Single-file programs and a tiny two-module program (~240 bytes) upload and run fine.
What was measured
Where the corruption lands. Using pybricksdev's compile_multi_file output for a 2-module program (main + a ~3.8 KB module):

The string pybricks.tools sits at offsets 232–245 of the uploaded blob.
The hub read it as pybricksk\x00\x1cols. Bytes 240–242 were replaced by bytes 229–231 of the same blob (k\x00\x1c), a copy of the data 11 bytes earlier.
With the default write size, the payload per write is 507 bytes (5-byte command header).
Possible cause (my interpretation, not verified). Payload byte 240 is at about byte 252 of the L2CAP SDU (4 + 3 + 5 + 240). That is just after the first 251-byte LL data PDU. So the error appears to sit at a link-layer fragment boundary inside a single large ATT write.

Write size test. I wrapped pybricksdev to lower _max_write_size to 200, so each write fits in one LL PDU:

The same ~45–49 KB program then uploaded and ran correctly in every run (more than 10 uploads on v4.1.0 and v4.1.1).
In those runs, each module's data was also padded to start at a 4-byte aligned offset.
Padding alone, with the default write size, did not fix it.
On v4.1.1, the default write size still fails with ValueError: invalid syntax for number. 200-byte writes load all modules.
Steps to reproduce
A multi-file project of about 45 KB, for example a main file importing several modules of 1–30 KB each.
Upload and run it over Bluetooth from Pybricks Code, or with pybricksdev run ble.
It fails during an import with one of the errors above. Uploading the same files over USB works.
Notes
I can't rule out the PC's Bluetooth adapter or driver. The MediaTek MT7921 is the only adapter I have.
I can share the project files or run tests if that helps.

Activity

  1. added
    triage
    on Oct 23, 2026
  2. laurensvalk commented on Oct 23, 2026

    @laurensvalk
    Member

    Thanks! I've spent some time trying to reproduce this but was unable so far. Since it happens consistently for you, could you please make a copy of that version of your files?

    And capture traffic with https://bleak.readthedocs.io/en/latest/troubleshooting.html#capture-bluetooth-traffic ?

    This should tell us if a packet was missing or corrupted somehow.

  3. added
    topic: bluetooth
    software: pybricks-micropython
    and removed
    triage
    on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    software: pybricks-micropythontopic: bluetooth

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions