Command Sending Error Fix
Troubleshooting
COMMAND-SENDING-ERROR-FIX.md
Command Sending Error Fix
Problem
When sending the command "hi" (or any command) from the Public UI to the API, an error occurs. The command fails to get a proper response from the Minecraft Bedrock server.
Root Cause
The issue was in the SendLineAndReadResponseAsync method in RunnerHostedService.cs. The original implementation had several problems:
- Race Condition: The method tried to attach a new
OutputDataReceivedevent handler dynamically, but this conflicted with the existing handler already attached inStartAsync() - Event Handler Conflict: Multiple handlers were competing to consume the same output stream, causing responses to be missed
- Unreliable Response Capture: The temporary event handler approach was fundamentally flawed
Solution
Replaced the event-handler-based approach with a log monitoring approach:
Key Changes
- Log Monitoring Instead of Event Handlers: Monitor the
_logBuilderwhich is already being populated - Baseline Capture: Store the current log length before sending the command
- Polling Approach: Poll every 100ms to check for new output
- Increased Timeout: Changed from 3000ms to 5000ms
- Better Error Handling: Added proper logging and helpful timeout messages
- Thread Safety: Uses the existing
_lock
Benefits
- No conflicts with existing event handlers
- More reliable output capture
- Better debugging with logging
- Graceful timeout handling
Files Modified
MCBDS.API/Background/RunnerHostedService.cs- FixedSendLineAndReadResponseAsyncmethod
Build Status
Build successful - Ready to test