machine/usb/msc: BlockDevice.ReadAt is called from the USB interrupt
machine/usb/msc calls BlockDevice.ReadAt from the USB interrupt handler. Writes and unmaps are already moved to the processTasks goroutine (src/machine/usb/msc/msc.go:120), but reads are not. txHandler and rxHandler (msc.go:174, msc.go:184) call m.run, which handles READ(10) through scsiRead and readBlock, and that calls m.dev.ReadAt (scsi_readwrite.go:75) in interrupt context.
This is not documented, so a normal machine.BlockDevice can hang the board when used with MSC. Things that are fine in ReadAt from a goroutine but break in an interrupt include:
defer, which is not supported in interrupts (#3225)- a
sync.Mutexshared with code that also uses the device - heap allocation
I hit this with a flash disk wrapper whose ReadAt used defer to release a lock. On a XIAO RP2350 the host saw the device and got the INQUIRY reply, then the first READ(10) hung the chip. No block device appeared and the board later dropped off USB. Exposing the same flash through a ReadAt without defer worked, as does src/examples/usb-storage.
Moving reads to processTasks like writes would let any BlockDevice work with MSC. If reads must stay in the interrupt, it would help to document that ReadAt is called in interrupt context so implementations avoid defer, locks and allocation.
TinyGo dev at fed7ac03.
Source: tinygo-org/tinygo