Edit 3: This bug is no longer present in Version 14.1.
It appears to have been fixed in either Version 14.0 or Version 14.1.
Edit 1: A support case with the identification [CASE:5059026] was created.
Edit 2: This has just been confirmed as a bug by Wolfram Research.
I have just heard back from Wolfram Research. They have confirmed that this is a bug, for which the only workaround at this time is to revert to Mathematica 13.2.1.
If you need socket functionality in Mathematica, you are advised not to upgrade to the latest version.
Original post
The latest version, Mathematica 13.3, appears to break socket code that used to work in Mathematica 13.2.1.
Consider the following TCP server in Python:
##### BEGIN PYTHON TCP SERVER
import socketserver
import time
import json
class TCPHandler(socketserver.BaseRequestHandler):
def handle(self):
try:
bufSz = 2048
self.data = self.request.recv(bufSz)
if self.data:
jsonReq = json.loads(self.data.decode("utf-8").strip())
print(f"Request: {jsonReq}")
time.sleep(10.0) # <<<=== Emulating a long calculation
jsonRes = json.dumps({
"msg": jsonReq["msg"],
"msgLen": len(jsonReq["msg"])
})
print(f"Response: {jsonRes}")
self.request.sendall(bytes(jsonRes, "utf-8"))
except Exception as e:
print("Error: ", e)
if __name__ == "__main__":
host, port = "localhost", 10001
with socketserver.TCPServer((host, port), TCPHandler) as server:
print(f"Test server now running at {host} on port {port}")
server.serve_forever()
##### END PYTHON TCP SERVER
One may check that the setup is correct by employing the following client code in Python:
##### BEGIN PYTHON TCP CLIENT
import socket
import json
if __name__ == "__main__":
bufSz = 2048
host, port = "localhost", 10001
data = json.dumps({"msg": "Hello, Earth!"})
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((host, port))
sock.sendall(bytes(data + "\n", "utf-8"))
print(f"Sent: {data}")
recvd = str(sock.recv(bufSz), "utf-8")
print(f"Received: {recvd}")
##### END PYTHON TCP CLIENT
The following Wolfram Language client worked in Mathematica 13.2.1, but does not in Mathematica 13.3:
(* BEGIN WOLFRAM LANGUAGE CLIENT *)
ClearAll[sock];
sock = SocketConnect[{"localhost", 10001}]
ClearAll[dat];
dat = ExportString[<|"msg" -> "Hello, World!"|>, "JSON"];
Module[{res},
WriteString[sock, dat];
res = ByteArrayToString @ SocketReadMessage @ sock;
res
]
(* END WOLFRAM LANGUAGE CLIENT *)
I should note that if I remove the 10-second delay from the Python server, the Wolfram Language client works just fine. But with any sort of delay, it does not. The request is sent correctly, but the response is not received. The code just hangs.
I tried to fix the client by writing the module in the following way.
Module[{res},
WriteString[sock, dat];
res = Reap[While[SocketReadyQ @ sock, Sow[SocketReadMessage @ sock]]];
res
]
But the Wolfram Language client still hangs.
Any idea what could be happening?
Thanks in advance.