fix(llmcore): add thread lock to reload_mykeys + remove non-ASCII chars#337
Open
feision wants to merge 3 commits into
Open
fix(llmcore): add thread lock to reload_mykeys + remove non-ASCII chars#337feision wants to merge 3 commits into
feision wants to merge 3 commits into
Conversation
- Add _RESYNC_LOCK = threading.RLock() for cross-module synchronization - Replace import+reload with compile()+exec() in _load_mykeys() to avoid Python import lock contention during Streamlit hot-reload - Add retry logic (3 attempts, 100ms interval) for transient syntax errors - Enhanced error messages with line context and leading-space details Fixes IndentationError when Streamlit file watcher triggers concurrent reloads of mykey.py on Windows. Previous implementation used importlib reload which is not thread-safe and could fail when multiple threads attempt to parse partial file writes.
- Import _RESYNC_LOCK from llmcore - Wrap entire session-loading logic in 'with _RESYNC_LOCK:' block - Ensures reload_mykeys() never called concurrently from multiple Streamlit fragments or agent threads This complements the llmcore fix and eliminates race conditions during hot-reload scenarios.
- Replace 'import mykey' with 'from llmcore import reload_mykeys' - Direct import bypasses thread-safe loader causing IndentationError - Wrap returned dict in ConfigNamespace for backward compatibility Ensures vision API calls respect thread-safety mechanism and prevents concurrent access issues in multi-session environments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🐛 Problem
When running GenericAgent with Streamlit frontend on Windows, users encounter
IndentationErrorduring hot-reload:Root causes identified:
mykey_template.pycontains UTF-8 box-drawing characters (═╔║─│etc.) used for visual decoration. These non-ASCII characters can interfere with Python'scompile()parser in certain encoding environments (especially Windows console withcp1252), causing misreported line numbers and false indentation errors.llmcore.reload_mykeys()is not thread-safe. It usesimport mykey; importlib.reload(mykey)which relies on Python's import lock. Under Streamlit's file-watcher trigger, multiple threads may attempt to reload simultaneously, leading to race conditions and partial reads ofmykey.py.mixin_configdefault value inmykey_template.pyreferences undefined'gpt-native', causing MixinSession initialization failure for beginners.✅ Solution
1. Add thread lock to
llmcore.py_RESYNC_LOCK = threading.RLock()global lockimport+reloadwithcompile()+exec()to bypass import mechanism entirely2. Protect
agentmain.load_llm_sessions()with _RESYNC_LOCK:blockreload_mykeys()is never called concurrently3. Fix
memory/vision_api.template.pyimport mykeytofrom llmcore import reload_mykeys🔍 Testing
Verified on Windows 10 + Python 3.10 + Streamlit 1.57.0:
No
IndentationErrorafter 50+ reload cycles.📊 Impact
IndentationErrorcrash on Windows + Streamlit🐛 问题
在 Windows 上使用 Streamlit 前端运行 GenericAgent 时,热重载期间会遇到
IndentationError:识别到的根本原因:
mykey_template.py包含 UTF-8 盒型绘图字符(═╔║─│等),用于视觉装饰。这些非 ASCII 字符在某些编码环境(特别是 Windows 控制台的cp1252编码)下会干扰 Python 的compile()解析器,导致行号错位和虚假的缩进错误。llmcore.reload_mykeys()不是线程安全的。它使用import mykey; importlib.reload(mykey),依赖于 Python 的导入锁。在 Streamlit 文件监视器触发时,多个线程可能同时尝试重载,导致竞争条件和mykey.py的部分读取。mykey_template.py中mixin_config的默认值引用了未定义的'gpt-native',导致初学者 MixinSession 初始化失败。✅ 解决方案
1. 为
llmcore.py添加线程锁_RESYNC_LOCK = threading.RLock()全局锁compile()+exec()替换import+reload,完全绕过导入机制2. 保护
agentmain.load_llm_sessions()with _RESYNC_LOCK:包裹reload_mykeys()永远不会并发调用3. 修复
memory/vision_api.template.pyimport mykey改为from llmcore import reload_mykeys🔍 测试验证
在 Windows 10 + Python 3.10 + Streamlit 1.57.0 环境验证:
经过 50+ 次重载 cycles,未再出现
IndentationError。📊 影响
IndentationError崩溃