{"record":{"id":"da2f9b942e0e7a30","repo":"neon-bindings/neon","slug":"context-is-not-available-in-async-functions-try-a-channel-da2f9b","errorCode":null,"errorMessage":"Context is not available in async functions. Try a `Channel` instead.","messagePattern":"Context is not available in async functions\\. Try a `Channel` instead\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/neon-macros/src/export/function/mod.rs","lineNumber":279,"sourceCode":"        // Provided `&mut Channel` instead of `Channel`\n        syn::Type::Reference(ty) if opts.context || is_channel_type(&ty.elem) => {\n            Err(syn::Error::new(\n                ty.span(),\n                \"Expected an owned `Channel` instead of a reference.\",\n            ))\n        }\n\n        // Provided a `&mut Cx` instead of a `Channel`\n        syn::Type::Reference(ty) if is_context_type(&ty.elem) => Err(syn::Error::new(\n            ty.elem.span(),\n            \"Expected an owned `Channel` instead of a context reference.\",\n        )),\n\n        // Found a `Channel`\n        _ if opts.context || is_channel_type(&ty.ty) => Ok(true),\n\n        // Tried to use an owned `Cx`\n        _ if is_context_type(&ty.ty) => Err(syn::Error::new(\n            ty.ty.span(),\n            \"Context is not available in async functions. Try a `Channel` instead.\",\n        )),\n\n        _ => Ok(false),\n    }\n}\n\n// Extract the first argument, that may be a context, of a function\nfn first_arg<'a>(\n    opts: &meta::Meta,\n    sig: &'a syn::Signature,\n) -> syn::Result<Option<&'a syn::PatType>> {\n    // Extract the first argument\n    let arg = match sig.inputs.first() {\n        Some(arg) => arg,\n\n        // If context was forced, error to let the user know the mistake","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/neon-bindings/neon/blob/38960e4381d9ad13b551cdf2d261f609167c9bc2/crates/neon-macros/src/export/function/mod.rs#L261-L297","documentation":"Neon's export macro rejects any attempt to use the execution context (`Cx`/context type) in an `async` exported function, because the JS context is not available on the async executor's thread. Instead, Neon requires a `Channel` to schedule work back on the JavaScript thread. `check_channel` fires this error when an owned (non-reference) parameter is a context type.","triggerScenarios":"Declaring `#[neon::export] async fn f(cx: Cx)` — an owned context type parameter in an async fn, matched by `_ if is_context_type(&ty.ty)`.","commonSituations":"Writing async exports for the first time and copying the sync signature (`cx: &mut Cx`) while dropping the reference; trying to execute JS work directly inside a `Future` running off the main thread.","solutions":["Replace the `Cx` parameter with an owned `Channel`: `async fn f(ch: Channel)`.","Use `ch.send(...)` or clone the channel into the future to hop back to the JS thread when you need to run JS work.","If you genuinely need the context, convert the export to a synchronous function."],"exampleFix":"// before\n#[neon::export]\nasync fn fetch(cx: Cx, url: String) -> JsResult<JsString> { /* ... */ }\n\n// after\n#[neon::export]\nasync fn fetch(ch: Channel, url: String) -> JsResult<JsString> { /* ... */ }","handlingStrategy":"fallback","validationCode":"// Reject Cx params in async exports before compiling:\n// prefer: async fn f(ch: Channel)   — schedule JS work via ch.send()\n// avoid: async fn f(cx: Cx)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["In async exported functions, only take `Channel`; send closures back to the JS thread with `channel.send`.","Convert to a synchronous export if you truly need `&mut Cx`.","Clone the `Channel` into spawned futures/tasks rather than borrowing anything context-like."],"tags":["rust","neon","compile-time","macro","async","context","channel"],"backgroundTag":"unsupported-operation","analyzedSha":"38960e4381d9ad13b551cdf2d261f609167c9bc2","analyzedAt":"2026-09-13T09:05:33.640Z","contentChangedAt":"2026-09-13T09:05:33.640Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}