sqSocketListenOnPortBacklogSizeInterface does not test the
result of bind and of listen. Socket>>setPort: of a UDP socket uses
that function. On a port that another socket has, the image gets no
error, the socket stays unbound, and it receives nothing on that port.scripts/cogdiff/socket_bind_in_use.st, 2026-11-17): the
stock VM answers "no error" for the second setPort: of one UDP port, and
the second socket does not have the port. Our VM signals
SocketError: Address already in use. For TCP the image uses
primitiveSocketBindToPort, which tests the result, so the two VMs agree
there.bouraqadi/PharoMisc. Its tests
make two multicast sockets on one port and passed on the stock VM only
because the second bind failed with no error.sqSocketBindToPort does.SocketError: Address already in use for UDP on the patched Cog.A probe that prints the same lines on a correct VM. Run it with Pharo.image eval or file it in:
"socket_bind_in_use.st -- a second socket takes a port that a first socket
has, for UDP (setPort:) and for TCP (listenOn:backlogSize:).
scripts/vmrun.sh eval cog scripts/cogdiff/socket_bind_in_use.st
scripts/vmrun.sh eval cogp scripts/cogdiff/socket_bind_in_use.st
scripts/vmrun.sh eval ours scripts/cogdiff/socket_bind_in_use.st
The system refuses the second bind (Address already in use). The answer
shows if the image gets that error, and which port the second socket has.
Measured 2026-11-17 (Pharo 14 build 791):
stock Cog UDP: no error, and the second socket does not have the port.
TCP: SocketError: Address already in use.
our VM UDP and TCP: SocketError: Address already in use.
The stock VM does not test the result of bind in
sqSocketListenOnPortBacklogSizeInterface, which Socket>>setPort: uses."
| out try first second port |
out := WriteStream on: String new.
try := [ :block |
[ block value ifNil: [ 'no error' ] ]
on: Error
do: [ :e | e class name , ': ' , (e messageText ifNil: [ '' ]) asString ] ].
first := Socket newUDP.
second := Socket newUDP.
[ first setPort: 0.
port := first localPort.
out nextPutAll: 'UDP: the first socket has a port: '; print: port > 0; lf.
out nextPutAll: 'UDP: second socket, setPort: of the same port -> ';
nextPutAll: (try value: [ second setPort: port. nil ]); lf.
out nextPutAll: 'UDP: the second socket has the port of the first: ';
nextPutAll: (try value: [ (second localPort = port) printString ]); lf ]
ensure: [ first closeAndDestroy. second closeAndDestroy ].
first := Socket newTCP.
second := Socket newTCP.
[ first listenOn: 0 backlogSize: 4.
port := first localPort.
out nextPutAll: 'TCP: the first socket has a port: '; print: port > 0; lf.
out nextPutAll: 'TCP: second socket, listenOn: of the same port -> ';
nextPutAll: (try value: [ second listenOn: port backlogSize: 4. nil ]); lf.
out nextPutAll: 'TCP: the second socket has the port of the first: ';
nextPutAll: (try value: [ (second localPort = port) printString ]); lf.
out nextPutAll: 'TCP: the second socket waits for a connection: ';
nextPutAll: (try value: [ second isWaitingForConnection printString ]); lf ]
ensure: [ first closeAndDestroy. second closeAndDestroy ].
out contents
Names such as scripts/vmrun.sh and scripts/cogdiff/ are scripts of the iospharo repository, which runs one VM at a time. cog is stock Cog, cogp is Cog with the change, ours is iospharo.
A change of the Cog sources (pharo-project/pharo-vm, branch pharo-12), on the local branch fix/socket-listen-bind-result. State: committed on the branch, tested. Not sent upstream.
From 6361258724d1bc67541399678594d66a8338d592 Mon Sep 17 00:00:00 2001
From: Aaron Wohl
Date: Tue, 17 Nov 2026 11:23:42 -0500
Subject: [PATCH] SocketPlugin: report a failed bind or listen of the listen
primitives to the image
sqSocketListenOnPortBacklogSizeInterface did not test the result of bind
and of listen. Socket>>setPort: of a UDP socket on a port that another
socket has then gave no error, the socket stayed unbound, and it received
nothing on that port.
The function now records the socket error and fails the primitive, as
sqSocketBindToPort does. The image then signals SocketError with the text
of the system (Address already in use).
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
---
.../src/common/SocketPluginImpl.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/plugins/SocketPlugin/src/common/SocketPluginImpl.c b/plugins/SocketPlugin/src/common/SocketPluginImpl.c
index 34a69938f..15e93af7e 100644
--- a/plugins/SocketPlugin/src/common/SocketPluginImpl.c
+++ b/plugins/SocketPlugin/src/common/SocketPluginImpl.c
@@ -884,11 +884,26 @@ void sqSocketListenOnPortBacklogSizeInterface(SocketPtr s, sqInt port, sqInt bac
saddr.sin_family= AF_INET;
saddr.sin_port= htons((short)port);
saddr.sin_addr.s_addr= htonl(addr);
- bind(SOCKET(s), (struct sockaddr*) &saddr, sizeof(saddr));
+ if (bind(SOCKET(s), (struct sockaddr*) &saddr, sizeof(saddr)) < 0)
+ {
+ /* The port is in use, or the address is not an address of this
+ machine. Without this test the socket stayed unbound and the
+ image got no error: Socket>>setPort: of a UDP socket on a port
+ that another socket has answered with no error, and the socket
+ then received nothing on that port. */
+ PSP(s)->sockError= getLastSocketError();
+ success(false);
+ return;
+ }
if (TCPSocketType == s->socketType)
{
/* --- TCP --- */
- listen(SOCKET(s), backlogSize);
+ if (listen(SOCKET(s), backlogSize) < 0)
+ {
+ PSP(s)->sockError= getLastSocketError();
+ success(false);
+ return;
+ }
SOCKETSTATE(s)= WaitingForConnection;
aioEnable(SOCKET(s), PSP(s), 0);
aioHandle(SOCKET(s), acceptHandler, AIO_RX); /* R => accept() */
--
2.54.0 (Apple Git-157)
State of 2026-11-20. Made from the test records of the iospharo project (a second Pharo VM). The fixes are proposals for review.