| 1 | // This module defines the Context type, which carries deadlines, cancellation signals, |
| 2 | // and other request-scoped values across API boundaries and between processes. |
| 3 | // Based on: https://github.com/golang/go/tree/master/src/context |
| 4 | // Last commit: https://github.com/golang/go/commit/52bf14e0e8bdcd73f1ddfb0c4a1d0200097d3ba2 |
| 5 | module context |
| 6 | |
| 7 | import rand |
| 8 | import sync |
| 9 | import time |
| 10 | |
| 11 | pub type CancelFn = fn () |
| 12 | |
| 13 | pub interface Canceler { |
| 14 | id string |
| 15 | mut: |
| 16 | cancel(remove_from_parent bool, err IError) |
| 17 | done() chan int |
| 18 | } |
| 19 | |
| 20 | // A CancelContext can be canceled. When canceled, it also cancels any children |
| 21 | // that implement Canceler. |
| 22 | pub struct CancelContext { |
| 23 | id string |
| 24 | mut: |
| 25 | context Context |
| 26 | mutex &sync.Mutex = sync.new_mutex() |
| 27 | done chan int |
| 28 | children map[string]Canceler |
| 29 | err IError = none |
| 30 | } |
| 31 | |
| 32 | // with_cancel returns a copy of parent with a new done channel. The returned |
| 33 | // context's done channel is closed when the returned cancel function is called |
| 34 | // or when the parent context's done channel is closed, whichever happens first. |
| 35 | // |
| 36 | // Canceling this context releases resources associated with it, so code should |
| 37 | // call cancel as soon as the operations running in this Context complete. |
| 38 | pub fn with_cancel(mut parent Context) (Context, CancelFn) { |
| 39 | mut c := new_cancel_context(parent) |
| 40 | propagate_cancel(mut parent, mut c) |
| 41 | cancel_fn := fn [mut c] () { |
| 42 | c.cancel(true, canceled) |
| 43 | } |
| 44 | return Context(c), CancelFn(cancel_fn) |
| 45 | } |
| 46 | |
| 47 | // new_cancel_context returns an initialized CancelContext. |
| 48 | fn new_cancel_context(parent Context) &CancelContext { |
| 49 | return &CancelContext{ |
| 50 | id: rand.uuid_v4() |
| 51 | context: parent |
| 52 | mutex: sync.new_mutex() |
| 53 | done: chan int{cap: 2} |
| 54 | err: none |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // deadline always returns none for a CancelContext, since it has no deadline. |
| 59 | pub fn (ctx &CancelContext) deadline() ?time.Time { |
| 60 | return none |
| 61 | } |
| 62 | |
| 63 | // done returns the done channel, which is closed when this context is canceled. |
| 64 | pub fn (mut ctx CancelContext) done() chan int { |
| 65 | ctx.mutex.lock() |
| 66 | done := ctx.done |
| 67 | ctx.mutex.unlock() |
| 68 | return done |
| 69 | } |
| 70 | |
| 71 | // err returns `canceled` after the context has been canceled, or `none` if not yet canceled. |
| 72 | pub fn (mut ctx CancelContext) err() IError { |
| 73 | ctx.mutex.lock() |
| 74 | err := ctx.err |
| 75 | ctx.mutex.unlock() |
| 76 | return err |
| 77 | } |
| 78 | |
| 79 | // value returns the CancelContext itself if the key matches the cancel context key, |
| 80 | // otherwise delegates to the parent context. |
| 81 | pub fn (ctx &CancelContext) value(key Key) ?Any { |
| 82 | if key == cancel_context_key { |
| 83 | return ctx |
| 84 | } |
| 85 | return ctx.context.value(key) |
| 86 | } |
| 87 | |
| 88 | // str returns a string representation of the CancelContext, |
| 89 | // showing the parent context name suffixed with '.with_cancel'. |
| 90 | pub fn (ctx &CancelContext) str() string { |
| 91 | return context_name(ctx.context) + '.with_cancel' |
| 92 | } |
| 93 | |
| 94 | fn (mut ctx CancelContext) cancel(remove_from_parent bool, err IError) { |
| 95 | if err is none { |
| 96 | panic('context: internal error: missing cancel error') |
| 97 | } |
| 98 | |
| 99 | ctx.mutex.lock() |
| 100 | if ctx.err !is none { |
| 101 | ctx.mutex.unlock() |
| 102 | // already canceled |
| 103 | return |
| 104 | } |
| 105 | |
| 106 | ctx.err = err |
| 107 | |
| 108 | if !ctx.done.closed { |
| 109 | ctx.done <- 0 |
| 110 | ctx.done.close() |
| 111 | } |
| 112 | |
| 113 | for _, child in ctx.children { |
| 114 | // NOTE: acquiring the child's lock while holding parent's lock. |
| 115 | mut c := child |
| 116 | c.cancel(false, err) |
| 117 | } |
| 118 | |
| 119 | ctx.children = map[string]Canceler{} |
| 120 | ctx.mutex.unlock() |
| 121 | |
| 122 | if remove_from_parent { |
| 123 | mut cc := &ctx.context |
| 124 | remove_child(mut cc, ctx) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | fn propagate_cancel(mut parent Context, mut child Canceler) { |
| 129 | done := parent.done() |
| 130 | select { |
| 131 | _ := <-done { |
| 132 | // parent is already canceled |
| 133 | child.cancel(false, parent.err()) |
| 134 | return |
| 135 | } |
| 136 | else {} |
| 137 | } |
| 138 | mut p := parent_cancel_context(mut parent) or { |
| 139 | // Pre-extract the parent done channel and pass it by value into the |
| 140 | // spawn. The goroutine may outlive the caller's stack frame, so |
| 141 | // dereferencing `parent` inside the spawn (e.g. via parent.done()) |
| 142 | // reads freed stack memory once the caller returns and reuses that |
| 143 | // region — observed as a consistent segfault in vlib/context/cause_test.v |
| 144 | // on Linux x86_64. Mirrors the fix in onecontext.run_two_contexts. |
| 145 | spawn fn (pdone chan int, mut parent Context, mut child Canceler) { |
| 146 | select { |
| 147 | _ := <-pdone { |
| 148 | child.cancel(false, parent.err()) |
| 149 | } |
| 150 | } |
| 151 | }(done, mut parent, mut child) |
| 152 | return |
| 153 | } |
| 154 | |
| 155 | if p.err is none { |
| 156 | p.children[child.id] = child |
| 157 | } else { |
| 158 | // parent has already been canceled |
| 159 | child.cancel(false, p.err) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // parent_cancel_context returns the underlying CancelContext for parent. |
| 164 | // It does this by looking up parent.value(&cancel_context_key) to find |
| 165 | // the innermost enclosing CancelContext and then checking whether |
| 166 | // parent.done() matches that CancelContext. (If not, the CancelContext |
| 167 | // has been wrapped in a custom implementation providing a |
| 168 | // different done channel, in which case we should not bypass it.) |
| 169 | fn parent_cancel_context(mut parent Context) ?&CancelContext { |
| 170 | done := parent.done() |
| 171 | if done.closed { |
| 172 | return none |
| 173 | } |
| 174 | mut p := parent.value(cancel_context_key)? |
| 175 | match mut p { |
| 176 | CancelContext { |
| 177 | pdone := p.done() |
| 178 | if done == pdone { |
| 179 | return p |
| 180 | } |
| 181 | } |
| 182 | else {} |
| 183 | } |
| 184 | |
| 185 | return none |
| 186 | } |
| 187 | |
| 188 | // remove_child removes a context from its parent. |
| 189 | fn remove_child(mut parent Context, child Canceler) { |
| 190 | mut p := parent_cancel_context(mut parent) or { return } |
| 191 | p.children.delete(child.id) |
| 192 | } |
| 193 | |