A panic inside an interrupt handler hangs USB CDC targets without a message

オープン
#6,322 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
18/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
go

調査の方向性

The hang comes from putchar writing into machine.Serial (USBCDC), where USBCDC.Write calls gosched() and task.Pause then fails inside an interrupt. Start by reading the USBCDC Write path and internal/task.Pause, and reproduce with the four-case ISR program from the issue. Done means ISR panics and large prints produce output or a clean message without hanging, with the watchdog no longer required.

索引モデルが issue の本文から書いたものです。

説明

On targets that use USB CDC for stdout, a panic inside an interrupt handler prints nothing and the board hangs. A print inside an interrupt handler that does not fit in the USB TX ring also hangs the board. This only happens when a host has the serial port open.

Reproduce

Tested on a Waveshare RP2040-Zero (-target=waveshare-rp2040-zero) with the port open in a terminal. The watchdog resets the board after the hang, so that it can be flashed again.

package main

import (
	"device/rp"
	"machine"
	"runtime/interrupt"
	"time"
)

var mode = "panic" // panic, big or small

var nilp *int

func handler(interrupt.Interrupt) {
	rp.TIMER.INTR.Set(1 << 1)
	switch mode {
	case "panic":
		println("isr: about to panic")
		println(*nilp)
	case "big":
		println("isr: printing 600 bytes")
		for i := 0; i < 600; i++ {
			print("x")
		}
		println()
	case "small":
		println("isr: small print")
	}
}

func main() {
	machine.Watchdog.Configure(machine.WatchdogConfig{TimeoutMillis: 5000})
	machine.Watchdog.Start()
	time.Sleep(4 * time.Second)
	interrupt.New(rp.IRQ_TIMER_IRQ_1, handler).Enable()
	rp.TIMER.INTE.SetBits(1 << 1)
	rp.TIMER.ALARM1.Set(rp.TIMER.TIMERAWL.Get() + 100000)
	for i := 0; ; i++ {
		println("main alive", i)
		machine.Watchdog.Update()
		time.Sleep(500 * time.Millisecond)
	}
}

Results

Case Serial output Board
nil pointer panic in main (control) full panic message stops, as expected
nil pointer panic in the ISR nothing, not even the println before the panic hangs
600 byte print in the ISR nothing, not even the first line hangs
600 byte print in the ISR, port not open nothing runs
small print in the ISR arrives after the ISR returns runs

Cause

putchar writes to machine.Serial, which is USBCDC. The data goes into the TX ring, and the USB interrupt sends it. Inside an interrupt handler the USB interrupt cannot run.

  1. Panic: the message goes into the ring, then abort() loops on wfi in handler mode. The ring is never sent.
  2. Full ring: USBCDC.Write calls gosched() to wait for space. task.Pause sees interrupt.In() and calls runtimeFatal("blocked inside interrupt"). runtimeFatal prints through the same Write, which calls gosched() again.

When the port is not open, Write returns early and drops the data, so the board does not hang.

The same path exists for HardFault_Handler and for the nil and bounds check panics in runtime and machine interrupt handlers. It affects every target where stdout is USB CDC, for example samd21, samd51, rp2040, rp2350, nrf52840 and mimxrt1062.

How this was found

A prototype call graph check from interrupt handlers to runtime.alloc and internal/task.Pause reported the putchar → USBCDC.Write → runtime.Gosched path on all USB CDC targets. The prototype is on https://github.com/davecheney/tinygo/tree/isr-taint-prototype.

/cc @dgryski @rdon-key

主要言語
Go
スター
17.8k
フォーク
1.1k
平均マージ
1日 4時間
マージ済み PR(30日)
380

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

tinygo-org/tinygo のほかの issue

tinygo-org/tinygo の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。