Files
hakorune/docs/archive/socketbox_deadlock_issue.md
Moe Charm ef7a0de3b0 feat: Prepare for code modularization and cleanup
- Archive old documentation and test files to `docs/archive/` and `local_tests/`.
- Remove various temporary and old files from the project root.
- Add `nekocode-rust` analysis tool and its output files (`nekocode/`, `.nekocode_sessions/`, `analysis.json`).
- Minor updates to `apps/chip8_nyash/chip8_emulator.nyash` and `local_tests` files.

This commit cleans up the repository and sets the stage for further code modularization efforts, particularly in the `src/interpreter` and `src/parser` modules, based on recent analysis.
2025-08-16 01:30:39 +09:00

8.7 KiB
Raw Blame History

🚨 緊急Issue: SocketBoxメソッド呼び出しデッドロック問題

Issue Priority: 🔥 CRITICAL - 最高緊急度
Status: 🚨 SocketBox完全機能停止
Impact: Phase 9実装HTTPサーバー等が完全に使用不能
Discovery Date: 2025-08-14

📋 問題概要

SocketBoxのすべてのメソッドbind(), listen(), isServer(), toString()等)が無限ブロックし、一切の操作が不可能。

他のBox型StringBox, IntegerBox, ArrayBox等は正常動作 - SocketBox特有の問題。

🎯 詳細分析結果

正常動作確認済み

  • SocketBox作成: new SocketBox()
  • Clone機能: Arc参照共有 Arc addresses match = true
  • 状態管理: Arc内部状態正常

問題箇所特定済み

// src/interpreter/expressions.rs:462-464 (問題発生箇所)
if let Some(socket_box) = obj_value.as_any().downcast_ref::<SocketBox>() {
    let result = self.execute_socket_method(socket_box, method, arguments)?;
    // ↑↑↑ この行に到達しないexecute_socket_methodが呼ばれない

問題の核心: downcast_ref::<SocketBox>() または obj_value 取得段階でデッドロック

📊 実行ログ証拠

🔥 デッドロック再現ログ

[Console LOG] SocketBox作成完了
[Console LOG] bind実行開始...
🔥 SOCKETBOX CLONE DEBUG:
🔥   Original Socket ID = 12
🔥   Arc addresses match = true    # ← Clone処理正常
# ここで無限ブロック
# 🔥 SOCKET_METHOD: bind() called が出力されない

正常動作比較 (他のBox)

[Console LOG] ArrayBox作成完了
[Console LOG] push実行開始...
✅ ARRAY_METHOD: push() called    # ← 正常にメソッド到達
✅ ArrayBox push completed        # ← 正常完了

🧪 完全再現テストケース

Test 1: 最小再現ケース

# test_socket_deadlock_minimal.nyash
static box Main {
    init { console }
    main() {
        me.console = new ConsoleBox()
        local socket = new SocketBox()
        me.console.log("SocketBox作成成功")
        
        # ここで無限ブロック
        local result = socket.bind("127.0.0.1", 19999)  
        me.console.log("これは出力されない")
    }
}

実行コマンド: timeout 10s ./target/release/nyash test_socket_deadlock_minimal.nyash 期待結果: タイムアウト (デッドロック)

Test 2: 他メソッドでの動作確認

# test_socket_methods_comprehensive.nyash
static box Main {
    init { console }
    main() {
        me.console = new ConsoleBox()
        local socket = new SocketBox()
        
        # 全メソッドテスト
        local result1 = socket.isServer()    # デッドロック
        local result2 = socket.toString()    # デッドロック  
        local result3 = socket.close()       # デッドロック
        
        return "全て失敗"
    }
}

Test 3: 他のBox正常動作確認

# test_other_boxes_working.nyash
static box Main {
    init { console }
    main() {
        me.console = new ConsoleBox()
        
        # ArrayBox - 正常動作確認
        local array = new ArrayBox()
        array.push("test")
        me.console.log("ArrayBox正常: " + array.size().toString())
        
        # MapBox - 正常動作確認
        local map = new MapBox()
        map.set("key", "value")
        me.console.log("MapBox正常: " + map.get("key").toString())
        
        return "他のBoxは正常動作"
    }
}

実行コマンド: ./target/release/nyash test_other_boxes_working.nyash 期待結果: 正常完了

🔍 詳細調査要求 - 根本的原因特定

禁止: 場当たり的修正

  • symptom-based修正禁止
  • 推測による部分修正禁止
  • 表面的デバッグログ追加のみ禁止

要求: システマティック根本調査

Phase 1: アーキテクチャレベル分析

  • Mutex Chain分析: SocketBox特有のArcチェーンがデッドロック原因か
  • Memory Layout分析: SocketBox vs 他Boxのメモリ配置差異
  • Ownership Pattern分析: Arc参照パターンでの循環参照確認

Phase 2: コンパイラ・ランタイムレベル

  • Type System分析: SocketBox専用の型解決問題
  • Trait Resolution分析: downcast_refでのtrait解決スタック
  • Runtime Stack分析: メソッド解決でのスタックオーバーフロー確認

Phase 3: パーサー・AST・インタープリターレベル

  • Parser Level: SocketBoxメソッド呼び出しAST生成問題
  • AST Structure: SocketBox専用のAST構造異常
  • Interpreter Pipeline: 全実行パイプラインでのボトルネック特定

Phase 4: Box実装アーキテクチャ比較

// 系統的比較調査対象
StringBox   // ✅ 正常 - Arc<String>のみ  
ArrayBox    // ✅ 正常 - Arc<Mutex<Vec>>のみ
MapBox      // ✅ 正常 - Arc<Mutex<HashMap>>のみ
SocketBox   // ❌ 問題 - Arc<Mutex<TcpListener>> + Arc<Mutex<bool>> × 複数

仮説: SocketBox特有の複数Arc組み合わせが循環デッドロック原因

Phase 5: Concurrent Access Pattern分析

  • Lock Order: 複数Mutex取得順序問題
  • Recursive Lock: 同じMutex再帰ロック問題
  • Cross-Reference: Arc間の相互参照デッドロック

⚙️ デバッグ環境

ビルド設定

cargo build --release -j32

実行環境

  • Platform: WSL2 Linux
  • Rust: latest stable
  • Nyash: PR #75マージ後

追加デバッグログ

以下のログが既に追加済み:

  • SocketBox Clone処理: 動作
  • execute_socket_method: 到達しない
  • 他Boxメソッド: 動作

🎯 完了条件

必須達成項目

  1. SocketBox.bind()正常動作:

    local result = socket.bind("127.0.0.1", 8080)
    # result.equals(true) == true
    
  2. SocketBox.isServer()正常動作:

    socket.bind("127.0.0.1", 8080)
    local isServer = socket.isServer()
    # isServer.equals(true) == true  
    
  3. SocketBox.toString()正常動作:

    local socketStr = socket.toString()  
    # デッドロックなし・文字列返却
    

テスト実行必須

# 基本動作テスト
./target/release/nyash test_socket_deadlock_minimal.nyash
# 期待結果: 正常完了・デッドロックなし

# 包括的機能テスト  
./target/release/nyash test_socket_methods_comprehensive.nyash
# 期待結果: 全メソッド正常動作

# 状態保持テスト
./target/release/nyash test_socket_state_preservation.nyash  
# 期待結果: bind() → isServer() == true

📊 構造的分析ツール提供

Architecture Comparison Script

# Box構造比較スクリプト作成推奨
rg "struct.*Box" src/boxes/ -A 10 > box_structures.txt
rg "Arc<Mutex" src/boxes/ > arc_mutex_patterns.txt  
rg "impl.*for.*Box" src/boxes/ > box_implementations.txt

Deadlock Detection Strategy

// 推奨調査コード
// src/boxes/socket_box.rs に一時的追加
impl SocketBox {
    fn debug_mutex_state(&self) {
        eprintln!("🔍 MUTEX STATE:");
        eprintln!("  listener strong_count: {}", Arc::strong_count(&self.listener));
        eprintln!("  is_server strong_count: {}", Arc::strong_count(&self.is_server));
        eprintln!("  thread_id: {:?}", std::thread::current().id());
    }
}

📝 報告要求 - システマティック

必須分析項目

  1. Root Cause Architecture - システムレベルでの構造的問題特定
  2. Comparative Analysis - 他Boxとの決定的差異メモリ・型・実装
  3. Pipeline Bottleneck - パーサー→AST→インタープリター→実行の問題段階
  4. Concurrency Model - Arcモデルでのデッドロック形成メカニズム
  5. Fix Strategy - 根本解決戦略(アーキテクチャ変更含む)

技術実証要求

  • Before/After比較: 修正前後の詳細動作比較
  • Performance Impact: 修正による他機能への性能影響
  • Memory Safety: 修正がメモリ安全性に与える影響
  • Concurrent Safety: 修正が並行安全性に与える影響

厳格禁止事項

  • Surface-level修正: 症状のみ修正・根本原因放置
  • Guesswork Solutions: 実証なしの推測ベース修正
  • Partial Validation: 一部テストのみで完了報告
  • Architecture Debt: 技術負債を生む応急処置

🚨 この問題はPhase 9HTTPサーバー実装の完全阻害要因です。最優先で完全解決をお願いします。