Issue1806

classification
Title: socket.getsockname() doesn't return a sensible value until socket.listen() has been called
Type: behaviour Severity: normal
Components: Library Versions: 2.5.2
Milestone:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: amak Nosy List: amak, irmen
Priority: Keywords:

Created on 2011-10-09.23:11:55 by irmen, last changed 2011-10-15.10:31:47 by amak.

Messages
msg6666 (view) Author: Irmen de Jong (irmen) Date: 2011-10-09.23:11:54
socket.getsockname() doesn't return a sensible value until socket.listen() has been called.

CPython's socket.getsockname() also works without calling listen().

Reproduce session:
[E:\projects\jython]dist\bin\jython.bat
Jython 2.6a0+ (, okt 8 2011, 17:15:54)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_26
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> s=socket.socket()
>>> s.bind(('',0))
>>> s.getsockname()
(u'127.0.0.1', 0)            # <-- hmmm
>>> s.listen(5)
>>> s.getsockname()
(u'0.0.0.0', 56068)          # <-- correct ip and (random) port number
>>>
msg6672 (view) Author: Irmen de Jong (irmen) Date: 2011-10-12.22:29:04
This is caused by the fact that the socket object doesn't have a sock_impl yet. That is only constructed later (in the listen method). Before that, getsockname() uses a meaningless hardcoded name and portnumber ("",0).

I've looked into it a bit and there doesn't seem an obvious way to fix the creation order of the sock_impl, because of the way Java's Socket objects are implemented (you have to pass the listen backlog size to the bind method, there doesn't seem to be a separate listen method).
However: IMO the getsockname method should create a sock_impl if it doesn't exist yet, with a 'guessed' listen backlog size (probably the default, 50) and return a sensible addresss+port, rather than the current behavior where a useless name+port is returned.
Thoughts?
msg6676 (view) Author: Alan Kennedy (amak) Date: 2011-10-15.10:31:47
This is a known limitation of jython's socket module that is not going to change.

http://wiki.python.org/jython/NewSocketModule#Deferred_socket_creation_on_jython

The only thing that can be changed is the interim value returned from getsockname(): if you have a suggestion for more meaningful value, then feel free to propose it.

Guessing, by creating a socket when the user calls getsockname(), is the wrong thing to do.

It is still unknown at that stage if the socket is a client or a server socket: guessing which one it will be is not possible. (both client and server sockets can be bound to a local address: it is just not required for client sockets).

If you want to propose a different return value for getsockname in these circumstances, re-open this issue.
History
Date User Action Args
2011-10-15 10:31:47amaksetstatus: open -> closed
assignee: amak
resolution: wont fix
messages: + msg6676
nosy: + amak
2011-10-12 22:29:05irmensetmessages: + msg6672
2011-10-09 23:11:55irmencreate