Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ impl Context {
}

pub fn get_func(&self, name: &str) -> Option<Arc<InnerFunction>> {
let value = self.get(name)?;
match value {
ContextValue::Function(func) => Some(func),
let binding = self.0.lock().unwrap();
match binding.get(name)? {
ContextValue::Function(func) => Some(func.clone()),
ContextValue::Variable(_) => None,
}
}

pub fn get_variable(&self, name: &str) -> Option<Value> {
let value = self.get(name)?;
match value {
ContextValue::Variable(v) => Some(v),
let binding = self.0.lock().unwrap();
match binding.get(name)? {
ContextValue::Variable(v) => Some(v.clone()),
ContextValue::Function(_) => None,
}
}
Expand All @@ -51,13 +51,15 @@ impl Context {
}

pub fn value(&self, name: &str) -> Result<Value> {
// Lock is acquired and released inside `get` early to avoid contention during execution
let value = self.get(name);
match value {
Some(ContextValue::Variable(v)) => Ok(v),
Some(ContextValue::Function(func)) => func(Vec::new()),
None => Ok(Value::None),
}
let func = {
let binding = self.0.lock().unwrap();
match binding.get(name) {
Some(ContextValue::Variable(v)) => return Ok(v.clone()),
Some(ContextValue::Function(func)) => func.clone(),
Comment on lines +54 to +58

Copilot AI Apr 6, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context::value now intentionally drops the MutexGuard before invoking a stored function (to avoid re-entrant deadlocks). Since this is a behavioral/concurrency fix, please add a regression test that would have deadlocked previously: store a function that calls back into the same Context (e.g., ctx.value("a")), call ctx.value("f") on another thread, and assert it completes via recv_timeout within a short duration.

Copilot uses AI. Check for mistakes.
None => return Ok(Value::None),
}
};
func(Vec::new())
}
}

Expand Down
Loading