runtime: time.Sleep can return before the duration ends

Đang mở
#6,519 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
go, wasm

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Go
Star
17.8k
Fork
1.1k
Merge trung bình
1 ngày 4 giờ
Pull request đã merge (30 ngày)
380

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của tinygo-org/tinygo

Tất cả issue của tinygo-org/tinygo

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.