Skip to content

Server stub can bind in a ephemeral available port but bloked by the firewall #48

@oseasandrepro

Description

@oseasandrepro

Discussed in #43

Originally posted by oseasandrepro October 22, 2025

    def __listen_for_func(self, func_name):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            s.bind((self.__host, 0))
            port = s.getsockname()[1]
            self.__register_func_in_binder(func_name, port)
            s.listen()
            s.settimeout(1.0)

            while not self.__stop_event.is_set():
                try:
                    conn, addr = s.accept()
                    self.__executor.submit(self.__handle_request, func_name, conn, addr)
                except socket.timeout:
                    continue
                except Exception as e:
                    self.__logger.error(f"An error occurred while listening for function [{{func_name}}] in port [{{port}}]: {{e}}")
                    os._exit(1)

In the above piece of code, in line: s.bind((self.__host, 0)), You’re asking the OS to bind the socket to any available ephemeral port (port chosen automatically by the kernel). This method is used to lunch a listener for each function in a different port.

It happens that available port is different to blocked/not blocked.
So, the SO can bind an available port that is blocked by the firewall.
In this scenario, the kernel(SO) will happily bind; the firewall will politely ignore the clients.

Proposed solution
Starting from the premise that, TCP protocol can handle multiple requests in the same port(one request on socket do handle it),
we can use only one port for all procedures - this is compliant with "RFC 1057" specification.
And maintain with the multithread approach (where each call for a procedure is handle in one thread)

References
How can a web server handle multiple user's incoming requests at a time on a single port (80)?
How Does a Web Server Handle Concurrent Requests on a Single Port?
How sockets work

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions