Skip to content

Conversation

@the-dev-z
Copy link
Member

Problem

  • Program crashes with log.Fatalf when WebSocket connection fails
  • Triggered by WebSocket hijacking issue (157.240.12.50)
  • Introduced in commit 3b1db6f (K-line WebSocket migration)

Solution

  • Replace 4x log.Fatalf with log.Printf in monitor.go
  • Lines 177, 183, 189, 215
  • Program now logs error and continues running

Changes

  1. Initialize failure: Fatalf → Printf (line 177)
  2. Connection failure: Fatalf → Printf (line 183)
  3. Subscribe failure: Fatalf → Printf (line 189)
  4. K-line subscribe: Fatalf → Printf + dynamic period (line 215)

Fallback

  • System automatically uses API when WebSocket cache is empty
  • GetCurrentKlines() has built-in degradation mechanism
  • No data loss, slightly slower API calls as fallback

Impact

  • ✅ Program stability: Won't crash on network issues
  • ✅ Error visibility: Clear error messages in logs
  • ✅ Data integrity: API fallback ensures K-line availability

Related: websocket-hijack-fix.md, auto-stop-bug-analysis.md

Pull Request: Fix WebSocket Crash (Clean Version)

🎯 PR信息

分支: fix/websocket-crash
目标: NoFxAiOS/nofxdev
来源: zhouyongyou/nofxfix/websocket-crash

Problem

Program crashes when WebSocket connection fails, causing complete system shutdown.

User report: "程序运行一会就自动停止了" (Program auto-stops after running for a while)

Root Cause

4 instances of log.Fatalf in market/monitor.go cause the entire program to exit when WebSocket operations fail:

  • Line 124: Initialize failure
  • Line 130: Connection failure
  • Line 136: Subscribe failure
  • Line 162: K-line subscribe failure

These fatal errors were introduced in commit 3b1db6f during the K-line WebSocket migration.

Trigger

  • Network issues (ISP blocking, firewall)
  • WebSocket connection hijacking (e.g., to Facebook IP 157.240.12.50)
  • Temporary network instability

Solution

Replace log.Fatalf with log.Printf to allow graceful error handling:

// BEFORE (crashes entire program)
if err != nil {
    log.Fatalf("❌ 初始化币种: %v", err)
    return
}

// AFTER (logs error and continues)
if err != nil {
    log.Printf("❌ 初始化币种失败: %v", err)
    return
}

Fallback Mechanism

The system has built-in fallback to API when WebSocket fails:

// market/monitor.go:293-318
func (m *WSMonitor) GetCurrentKlines(...) {
    if !exists {
        // ✅ Automatically falls back to API
        klines, err := apiClient.GetKlines(symbol, _time, 100)
        // ... store in cache and continue
    }
}

This ensures K-line data remains available even when WebSocket fails.


Changes

Files Changed

market/monitor.go | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

Detailed Changes

-		log.Fatalf("❌ 初始化币种: %v", err)
+		log.Printf("❌ 初始化币种失败: %v", err)

-		log.Fatalf("❌ 批量订阅流: %v", err)
+		log.Printf("❌ 批量订阅流失败: %v", err)

-		log.Fatalf("❌ 订阅币种交易对: %v", err)
+		log.Printf("❌ 订阅币种交易对失败: %v", err)

-		log.Fatalf("❌ 订阅3m K线: %v", err)
+		log.Printf("❌ 订阅 %s K线失败: %v", st, err)

Additional improvement: Line 162 now shows dynamic K-line period (%s) instead of hardcoded "3m".


Impact

Before

  • ❌ Program crashes on WebSocket failure
  • ❌ Complete system shutdown
  • ❌ Requires manual restart

After

  • ✅ Program logs error and continues running
  • ✅ Automatically falls back to API for K-line data
  • ✅ System remains operational

Testing

  • Program continues running when WebSocket initialization fails
  • Program continues running when WebSocket connection fails
  • Program continues running when subscribe fails
  • K-line data still available via API fallback
  • Error messages clearly logged for debugging

Severity

Critical - This issue causes complete program failure in production environments with network restrictions.


Related

  • Issue: WebSocket connection hijacking (157.240.12.50 Facebook IP)
  • Analysis: /Users/sotadic/Documents/GitHub/auto-stop-bug-analysis.md
  • Introduced: commit 3b1db6f (K-line WebSocket migration)

---

## 🚀 提交信息

**Commit**: `a7155e2`
**Date**: Wed Nov 5 01:10:49 2025 +0800
**Author**: ZhouYongyou

fix(market): prevent program crash on WebSocket failure

Problem

  • Program crashes with log.Fatalf when WebSocket connection fails
  • Triggered by WebSocket hijacking issue (157.240.12.50)
  • Introduced in commit 3b1db6f (K-line WebSocket migration)

Solution

  • Replace log.Fatalf with log.Printf in monitor.go
  • Lines 177, 183, 189, 215
  • Program now logs error and continues running

Fallback

  • System automatically uses API when WebSocket cache is empty
  • GetCurrentKlines() has built-in degradation mechanism
  • No data loss, slightly slower API calls as fallback

Impact

  • ✅ Program stability: Won't crash on network issues
  • ✅ Error visibility: Clear error messages in logs
  • ✅ Data integrity: API fallback ensures K-line availability

Related: websocket-hijack-fix.md, auto-stop-bug-analysis.md


---

## 📊 统计

**改动统计**:
- **文件数**: 1
- **新增行**: 4
- **删除行**: 4
- **净改动**: 0 行(纯替换)

**改动位置**:
- `market/monitor.go:124` - Initialize failure
- `market/monitor.go:130` - Connection failure
- `market/monitor.go:136` - Subscribe failure
- `market/monitor.go:162` - K-line subscribe failure

---

## ✅ 检查清单

- [x] 只包含 WebSocket crash fix
- [x] 没有引入新功能
- [x] 没有无关改动
- [x] Commit message 清晰
- [x] 易于审查(只有4行)
- [x] 测试通过

---

## 🎯 下一步操作

1. **访问 PR 创建链接**:

dev...zhouyongyou:nofx:fix/websocket-crash


2. **复制 PR Description** (上面的内容)

3. **点击 "Create Pull Request"**

4. **等待审查和合并**

---

**分支**: `fix/websocket-crash` ✅
**状态**: 已推送到 origin
**大小**: 4 行改动(非常干净)🎉

## Problem
- Program crashes with log.Fatalf when WebSocket connection fails
- Triggered by WebSocket hijacking issue (157.240.12.50)
- Introduced in commit 3b1db6f (K-line WebSocket migration)

## Solution
- Replace 4x log.Fatalf with log.Printf in monitor.go
- Lines 177, 183, 189, 215
- Program now logs error and continues running

## Changes
1. Initialize failure: Fatalf → Printf (line 177)
2. Connection failure: Fatalf → Printf (line 183)
3. Subscribe failure: Fatalf → Printf (line 189)
4. K-line subscribe: Fatalf → Printf + dynamic period (line 215)

## Fallback
- System automatically uses API when WebSocket cache is empty
- GetCurrentKlines() has built-in degradation mechanism
- No data loss, slightly slower API calls as fallback

## Impact
- ✅ Program stability: Won't crash on network issues
- ✅ Error visibility: Clear error messages in logs
- ✅ Data integrity: API fallback ensures K-line availability

Related: websocket-hijack-fix.md, auto-stop-bug-analysis.md
@github-actions
Copy link

github-actions bot commented Nov 4, 2025

🤖 Advisory Check Results

These are advisory checks to help improve code quality. They won't block your PR from being merged.

📋 PR Information

Title Format: ✅ Good - Follows Conventional Commits
PR Size: 🟢 Small (8 lines: +4 -4)

🔧 Backend Checks

Go Formatting: ⚠️ Needs formatting

Files needing formatting
api/server.go
config/database.go
manager/trader_manager.go

Go Vet: ✅ Good
Tests: ✅ Passed

Fix locally:

go fmt ./...      # Format code
go vet ./...      # Check for issues
go test ./...     # Run tests

⚛️ Frontend Checks

Build & Type Check: ✅ Success

Fix locally:

cd web
npm run build  # Test build (includes type checking)

📖 Resources

Questions? Feel free to ask in the comments! 🙏


These checks are advisory and won't block your PR from being merged. This comment is automatically generated from pr-checks-run.yml.

@Icyoung Icyoung merged commit cf4b728 into NoFxAiOS:dev Nov 4, 2025
10 checks passed
ForeverInLaw pushed a commit to ForeverInLaw/nofx that referenced this pull request Nov 8, 2025
fix(market): prevent program crash on WebSocket failure
@the-dev-z the-dev-z deleted the fix/websocket-crash branch November 12, 2025 09:38
bebest2010 pushed a commit to bebest2010/nofx that referenced this pull request Nov 18, 2025
fix(market): prevent program crash on WebSocket failure
tinkle-community pushed a commit that referenced this pull request Nov 26, 2025
fix(market): prevent program crash on WebSocket failure
tinkle-community pushed a commit that referenced this pull request Nov 26, 2025
fix(market): prevent program crash on WebSocket failure
tinkle-community pushed a commit that referenced this pull request Dec 7, 2025
fix(market): prevent program crash on WebSocket failure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants