FIX: Resolve SSH session timeout and disconnection issues
Fixed SSH sessions being automatically removed after a few minutes of
inactivity, causing connection errors when users return to the interface.
## Problems Identified:
### 1. Aggressive Session Timeout
**Issue**: Sessions were cleaned up after only 10 minutes of inactivity
- Too short for typical user workflows
- No warning or graceful handling when timeout occurs
- Users would get connection errors without explanation
### 2. No Session Keepalive Mechanism
**Issue**: No frontend keepalive to maintain active sessions
- Sessions would timeout even if user was actively viewing files
- No periodic communication to extend session lifetime
- No way to detect session expiration proactively
### 3. Server-side SSH Configuration
**Issue**: While SSH had keepalive settings, they weren't sufficient
- keepaliveInterval: 30000ms (30s)
- keepaliveCountMax: 3
- But no application-level session management
## Technical Solution:
### **Extended Session Timeout**
```typescript
// Increased from 10 minutes to 30 minutes
session.timeout = setTimeout(() => {
fileLogger.info(`Cleaning up inactive SSH session: ${sessionId}`);
cleanupSession(sessionId);
}, 30 * 60 * 1000); // 30 minutes
```
### **Backend Keepalive Endpoint**
```typescript
// New endpoint: POST /ssh/file_manager/ssh/keepalive
app.post("/ssh/file_manager/ssh/keepalive", (req, res) => {
const session = sshSessions[sessionId];
session.lastActive = Date.now();
scheduleSessionCleanup(sessionId); // Reset timeout
res.json({ status: "success", connected: true });
});
```
### **Frontend Automatic Keepalive**
```typescript
// Send keepalive every 5 minutes
keepaliveTimerRef.current = setInterval(async () => {
if (sshSessionId) {
await keepSSHAlive(sshSessionId);
}
}, 5 * 60 * 1000);
```
## Session Management Flow:
**Before (Problematic):**
1. User connects → 10-minute countdown starts
2. User leaves browser open but inactive
3. Session times out after 10 minutes
4. User returns → "SSH session not found" error
5. User forced to reconnect manually
**After (Fixed):**
1. User connects → 30-minute countdown starts
2. Frontend sends keepalive every 5 minutes automatically
3. Each keepalive resets the 30-minute timeout
4. Session stays alive as long as browser tab is open
5. Graceful handling if keepalive fails
## Benefits:
✅ **Extended Session Lifetime**: 30 minutes vs 10 minutes base timeout
✅ **Automatic Session Maintenance**: Keepalive every 5 minutes
✅ **Transparent to User**: No manual intervention required
✅ **Robust Error Handling**: Graceful degradation if keepalive fails
✅ **Resource Efficient**: Only active sessions consume resources
✅ **Better User Experience**: No unexpected disconnections
Sessions now persist for the entire duration users have the file
manager open, eliminating frustrating timeout errors.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -93,11 +93,11 @@ function scheduleSessionCleanup(sessionId: string) {
|
||||
// Clear existing timeout
|
||||
if (session.timeout) clearTimeout(session.timeout);
|
||||
|
||||
// Set new timeout for 10 minutes of inactivity
|
||||
// Increase timeout to 30 minutes of inactivity
|
||||
session.timeout = setTimeout(() => {
|
||||
fileLogger.info(`Cleaning up inactive SSH session: ${sessionId}`);
|
||||
cleanupSession(sessionId);
|
||||
}, 10 * 60 * 1000); // 10 minutes
|
||||
}, 30 * 60 * 1000); // 30 minutes - increased from 10 minutes
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,6 +321,41 @@ app.get("/ssh/file_manager/ssh/status", (req, res) => {
|
||||
res.json({ status: "success", connected: isConnected });
|
||||
});
|
||||
|
||||
// SSH keepalive endpoint - extends session timeout and verifies connection
|
||||
app.post("/ssh/file_manager/ssh/keepalive", (req, res) => {
|
||||
const { sessionId } = req.body;
|
||||
|
||||
if (!sessionId) {
|
||||
return res.status(400).json({ error: "Session ID is required" });
|
||||
}
|
||||
|
||||
const session = sshSessions[sessionId];
|
||||
|
||||
if (!session || !session.isConnected) {
|
||||
return res.status(400).json({
|
||||
error: "SSH session not found or not connected",
|
||||
connected: false
|
||||
});
|
||||
}
|
||||
|
||||
// Update last active time and reschedule cleanup
|
||||
session.lastActive = Date.now();
|
||||
scheduleSessionCleanup(sessionId);
|
||||
|
||||
fileLogger.debug(`SSH session keepalive: ${sessionId}`, {
|
||||
operation: "ssh_keepalive",
|
||||
sessionId,
|
||||
lastActive: session.lastActive,
|
||||
});
|
||||
|
||||
res.json({
|
||||
status: "success",
|
||||
connected: true,
|
||||
message: "Session keepalive successful",
|
||||
lastActive: session.lastActive
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/ssh/file_manager/ssh/listFiles", (req, res) => {
|
||||
const sessionId = req.query.sessionId as string;
|
||||
const sshConn = sshSessions[sessionId];
|
||||
|
||||
Reference in New Issue
Block a user