std.Io.Threaded: rework cancellation #30033
No reviewers
Labels
No labels
abi/f32
abi/ilp32
abi/sf
accepted
arch/21k
arch/6502
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/bfin
arch/bpf
arch/colossus
arch/cris
arch/csky
arch/dlx
arch/epiphany
arch/fr30
arch/frv
arch/hexagon
arch/hppa
arch/hppa64
arch/ia64
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/mcore
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/ns32k
arch/nvptx
arch/or1k
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390x
arch/sh
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/tricore
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/x86
arch/x86_64
arch/xcore
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/android
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hurd
os/illumos
os/ios
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/tvos
os/uefi
os/visionos
os/wasi
os/watchos
os/windows
proposal
release notes
testing
zig cc
zig fmt
zig reduce
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
use case
No milestone
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
ziglang/zig!30033
Loading…
Reference in a new issue
No description provided.
Delete branch "cancellation"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Now, before a syscall is entered,
beginSyscallis called, which may returnerror.Canceled. After syscall returns, whether error or success,endSyscallis called. If the syscall returnsEINTRthencheckCancelis called before looping.requestCancelonly sends a signal to potentially interrupt a blocking syscall if the request arrived while the I/O implementation is making a syscall.A new option is added,
robust_cancelwhich toggles the retry behavior. I did this because retrying is not always needed for every application, and I observed it to be relatively slow.Musl libc has a neat undocumented extension,
PTHREAD_CANCEL_MASKED, which causes the next syscall to returnECANCELEDwhile cleverly avoiding race conditions with a special syscall mechanism. It's wonderful, it's beautiful, it should be immediately standardized by POSIX. However, it does not allow resetting the "canceled" boolean, which means it cannot be used with worker thread pooling. That could be addressed by callingpthread_create/pthread_destroyfor each async task, which may not be such a bad idea since it would reduce contention on the global mutex.Addresses but does not close #30049.
f0c7d112819ff0ac924c9ff0ac924ccd9d84927bI tuned the
requestCancelfunction with this program:This program completes in about 50 seconds on my computer, indicating that all the
doWorktasks were successfully canceled (otherwise it would have taken at least 5 minutes).On a heavily loaded Linux 6.17.5, I observed a maximum of 20 attempts not acknowledged before the timeout (including exponential backoff) was sufficient, despite the heavy load.
The time wasted here sleeping is mitigated by the fact that, later on, the system will likely wait for the canceled task, causing it to indefinitely yield until the canceled task finishes, and the task must acknowledge the cancel before it proceeds to that point.
std.Io.Threaded: fix the cancellation raceto std.Io.Threaded: rework cancellation93b45fe15cfed3f14367@andrewrk
sinon/zig@790013b460Tested with a simple,
fed3f14367ad71d52d54653dd118edbb3f56d5d5