From 4d2383d54ca190c50fa938bdb791953849465bd7 Mon Sep 17 00:00:00 2001 From: Caznix Date: Fri, 6 Dec 2024 16:20:16 -0500 Subject: [PATCH] fix tokio runtime stack overflow due to recursion --- engine/src/core/repl/commands.rs | 15 ++++++++++++++- test.zensh | 4 ++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/engine/src/core/repl/commands.rs b/engine/src/core/repl/commands.rs index dad0fa2..e9b756d 100644 --- a/engine/src/core/repl/commands.rs +++ b/engine/src/core/repl/commands.rs @@ -1,12 +1,18 @@ use std::{ffi::OsStr, process::Command}; - +use parking_lot::Mutex; +use lazy_static::lazy_static; use crate::core::repl::repl::evaluate_command; use super::COMMAND_LIST; +const MAX_RECURSION_DEPTH: usize = 500; // increasing this value WILL cause a stack overflow. attempt at your own risk - Caz + +lazy_static! { + static ref RECURSION_DEPTH: Mutex = parking_lot::Mutex::new(0); +} pub(crate) fn say_hello() -> anyhow::Result<()> { println!("Hello, World!"); @@ -41,6 +47,13 @@ pub(crate) fn help() -> anyhow::Result<()> { Ok(()) } pub(crate) fn exec(args: Vec) -> anyhow::Result<()> { + *RECURSION_DEPTH.lock() += 1; + if *RECURSION_DEPTH.lock() > MAX_RECURSION_DEPTH { + eprintln!("Maximum recursion depth reached. Aborting."); + *RECURSION_DEPTH.lock() = 0; + return Ok(()); + } + println!("Recursion depth: {}", *RECURSION_DEPTH.lock()); let file_path_str = &args[0]; let file_path = std::path::Path::new(file_path_str); println!("File path: {:#?}", file_path); diff --git a/test.zensh b/test.zensh index e69de29..0fbb7d4 100644 --- a/test.zensh +++ b/test.zensh @@ -0,0 +1,4 @@ +echo ping +echo pong +exec test.zensh +break \ No newline at end of file