In my experience with lighttpd's mod_wstunnel
, I have encountered a peculiar issue. When I send two messages in quick succession from the backend using a UNIX socket to communicate between lighttpd and the backend, I noticed that lighttpd logs show that it receives these messages as a single combined message.
However, if there is even a minimal delay of 1ms between the two write
calls to the UNIX socket, lighttpd receives the messages separately and forwards them as such to the final client.
To demonstrate this issue, I created a simple proof of concept setup:
The configuration file for lighttpd (which can be launched using lighttpd -D -f lighttpd-poc.conf
):
server.document-root = var.CWD
server.bind = "0.0.0.0"
server.port = 8042
... (additional configurations) ...
Furthermore, the HTML page being served where the problem manifests easily:
<!DOCTYPE html>
<script>
... (WebSocket setup script) ...
</script>
And finally, the simplified backend code responsible for handling websocket requests:
#include <iostream>
#include <thread>
#include <chrono>
using namespace std::chrono_literals;
int main()
{
... (backend setup) ...
while (true) {
... (logic for processing messages) ...
// std::this_thread::sleep_for(1ms); // Uncommenting this line resolves the issue.
... (sending second message) ...
}
}
return 0;
}
It's worth noting that uncommenting the std::this_thread::sleep_for(1ms)
line in the backend code eliminates the message combining issue. This problem leads to failed JSON parsing by the browser when navigating to the specified URL due to the merged messages being received.
While workarounds exist like utilizing libraries or including the sleep function in the code, identifying the root cause of this buffering phenomenon is crucial. Understanding why and where this buffering occurs in the kernel will provide insights into resolving the issue effectively. Additionally, exploring solutions beyond temporary fixes is essential for long-term stability and optimal performance.
If relevant, the version of lighttpd being used is 1.4.55.