runtime: time.Sleep can return before the duration ends

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

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

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

評価

難易度
4/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
go, wasm

調査の方向性

The rounding is in nanosecondsToTicks, shown in src/runtime/runtime_rp2.go, and the same pattern is said to exist in the other tick-based port files. ticks() and the scheduler's wakeup computation are the second source of error. Start by reading those three functions and checking how time.Sleep reaches them. Done means sleeps never return early on the affected ports, for example a 500 ns sleep waits at least one tick, while ports that keep nanoseconds are unchanged.

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

説明

The time.Sleep documentation says: "Sleep pauses the current goroutine for at least the duration d." On TinyGo, a sleep can end too early when the duration is not a whole number of runtime ticks.

The cause is in nanosecondsToTicks. Each port converts nanoseconds to ticks with an integer division that rounds down. For example, src/runtime/runtime_rp2.go has:

func nanosecondsToTicks(ns int64) timeUnit {
    return timeUnit(ns / 1000)
}

Thus, with a 1 µs tick:

  • time.Sleep(500 * time.Nanosecond) gives 0 ticks, so it does not sleep.
  • time.Sleep(1500 * time.Nanosecond) gives 1 tick, so it sleeps for less than 1.5 µs.

The error is up to one tick. The tick size is different on each port:

Tick Ports
~30.5 µs (32768 Hz RTC) nrf, atsamd21, atsamd51, fe310, avrtiny
10 µs tkey
1 µs ra4m1, rp2040, rp2350, mimxrt1062, nxpmk66f18
25 ns to 100 ns esp32, esp32s3, stm32, riscv-qemu, windows

On nrf, time.Sleep(20 * time.Microsecond) gives 0 ticks, so it returns at once.

There is a second source of error. ticks() also rounds down. The scheduler sets the wakeup to ticks() plus the sleep in ticks. If the sleep starts late in a tick, the wakeup comes up to one tick early.

Ports that keep timeUnit in nanoseconds (unix, wasm, avr, mgba, arm7tdmi) do not round in nanosecondsToTicks.

主要言語
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 を短くまとめたダイジェスト。