cancel_work_sync(9)
π cancel_work_sync β the Kernel API Manual, section 9, bestie
okay so we're in the Kernel API Manual today (section 9 of it, to be exact) and the page is cancel_work_sync, and honestly? she's that girl. let's get into it π
β¨ NAME β¨
cancel_work_sync β cancel a work and wait for it to finish π«Ά
β¨ SYNOPSIS β¨
bool cancel_work_sync (struct work_struct *work );
like, that's the whole vibe. one arg. returns a bool. clean. π
β¨ ARGUMENTS β¨
workβ the work to cancel β¨ that's it, that's the parameter, she's literally just the work item you're trying to shut down.
β¨ DESCRIPTION β¨
okay so what she does: she cancels the work and waits for its execution to finish. like she doesn't just say "you're cancelled" and dip β she waits for it to actually be done. main character energy honestly π
and bestie, this function is flexible. you can use it even if the work re-queues itself, or migrates to another workqueue. no drama. π¦
on return from this function, work is guaranteed to be not pending or executing on any CPU β as long as there aren't racing enqueues. that's the caveat, keep it in your pocket. β
π¨ not me abt to save you from a bug:cancel_work_sync(delayed_work->work)must not be used fordelayed_work's. that's illegal in the kernel friend group. usecancel_delayed_work_syncinstead. no cap. π
context rules (the vibe check you can't skip):
- must be called from a sleepable context if
workwas last queued on a non-BH workqueue π΄ - can also be called from non-hardirq atomic contexts, including BH, if
workwas last queued on a BH workqueue β‘
and the return?? she gives you a bool:
trueifworkwas pendingfalseotherwise
periodt. πΈ
β¨ SEE ALSO β¨
all the siblings and cousins and the whole extended family, listed in the kernel file ./kernel/workqueue.c π:
for_each_pool(9), for_each_pool_worker(9), for_each_pwq(9), worker_pool_assign_id(9), unbound_effective_cpumask(9), get_work_pool(9), worker_set_flags(9), worker_clr_flags(9), worker_enter_idle(9), worker_leave_idle(9), find_worker_executing_work(9), move_linked_works(9), assign_work(9), kick_pool_pick(9), kick_pool(9), wq_worker_running(9), wq_worker_sleeping(9), wq_worker_tick(9), wq_worker_last_func(9), wq_node_nr_active(9), wq_update_node_max_active(9), get_pwq(9), put_pwq(9), put_pwq_unlocked(9), pwq_tryinc_nr_active(9), pwq_activate_first_inactive(9), unplug_oldest_pwq(9), node_activate_pending_pwq(9), pwq_dec_nr_active(9), pwq_dec_nr_in_flight(9), try_to_grab_pending(9), work_grab_pending(9), insert_work(9), queue_work_on(9), select_numa_node_cpu(9), queue_work_node(9), queue_delayed_work_on(9), mod_delayed_work_on(9), queue_rcu_work(9), worker_attach_to_pool(9), worker_detach_from_pool(9), create_worker(9), set_worker_dying(9), idle_worker_timeout(9), idle_cull_fn(9), maybe_create_worker(9), manage_workers(9), process_one_work(9), process_scheduled_works(9), worker_thread(9), rescuer_thread(9), check_flush_dependency(9), insert_wq_barrier(9), flush_workqueue_prep_pwqs(9), __flush_workqueue(9), drain_workqueue(9), flush_work(9), flush_delayed_work(9), flush_rcu_work(9), cancel_delayed_work(9), cancel_delayed_work_sync(9), disable_work(9), disable_work_sync(9), enable_work(9), disable_delayed_work(9), disable_delayed_work_sync(9), enable_delayed_work(9), schedule_on_each_cpu(9), execute_in_process_context(9), free_workqueue_attrs(9), alloc_workqueue_attrs(9), init_worker_pool(9), put_unbound_pool(9), get_unbound_pool(9), wq_calc_pod_cpumask(9), apply_workqueue_attrs(9), unbound_wq_update_pwq(9), wq_adjust_max_active(9), destroy_workqueue(9), workqueue_set_max_active(9), workqueue_set_min_active(9), current_work(9), current_is_workqueue_rescuer(9), current_is_workqueue_mem_reclaim(9), workqueue_congested(9), work_busy(9), set_worker_desc(9), print_worker_info(9), show_one_workqueue(9), show_one_worker_pool(9), show_all_workqueues(9), show_freezable_workqueues(9), rebind_workers(9), restore_unbound_workers_cpumask(9), work_on_cpu_key(9), freeze_workqueues_begin(9), freeze_workqueues_busy(9), thaw_workqueues(9), workqueue_unbound_housekeeping_update(9), workqueue_set_unbound_cpumask(9), workqueue_sysfs_register(9), workqueue_sysfs_unregister(9), workqueue_init_early(9), workqueue_init(9), llc_count_cores(9), llc_populate_cpu_shard_id(9), precompute_cache_shard_ids(9), workqueue_init_topology(9)
like... that's a whole group chat. iykyk. π
π«Ά the sign-off π«Ά
this lil page lives in ./kernel, stamped October 2026, and her name's cancel_work_sync(9). go cancel something (safely), bestie β mwah πβ¨π₯
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms