Skip to content

Added support for unix sockets#3152

Open
kSkip wants to merge 7 commits into
davisking:masterfrom
kSkip:unix-sockets
Open

Added support for unix sockets#3152
kSkip wants to merge 7 commits into
davisking:masterfrom
kSkip:unix-sockets

Conversation

@kSkip

@kSkip kSkip commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

I'm working on a Fast CGI server for my ML models and would like to integrate supporting changes into dlib's networking module. One of these changes is supporting socket type AF_UNIX, so web servers like nginx can bypass the network stack when communicating with the fcgi server. Another is a listener factory method that can accept connections on a user-provided socket. In many fast cgi setups, the application is expected to listen on stdin (which is bounded to a socket), so the user-provided socket is helpful here.

I implemented the required changes for both posix and win32 including tests. However, I'm not sure how to test the new create_listener_from_socket method. It would require platform specific code in the unit test.

Comment thread dlib/sockets/sockets_kernel_abstract.h Outdated
@davisking

Copy link
Copy Markdown
Owner

I implemented the required changes for both posix and win32 including tests. However, I'm not sure how to test the new create_listener_from_socket method. It would require platform specific code in the unit test.

How are you making the socket you have when you call create_listener_from_socket() in your real application? Can't just do basically that in the tests or even better have an abstracted generic method for doing it?

Co-authored-by: Davis E. King <davis685@gmail.com>
@kSkip

kSkip commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

How are you making the socket you have when you call create_listener_from_socket() in your real application?

For Fast CGI, you are usually not making the socket. Either systemd or another tool, like spawn-fcgi, will bind a socket to file descriptor 0 (stdin). So, I just pass 0 as the argument to create_listener_from_socket().

@kSkip

kSkip commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Can't just do basically that in the tests or even better have an abstracted generic method for doing it?

Yes, it's easy enough to just create, bind, and listen on a socket to use for the test, but I wasn't sure if putting a bunch of platform specific headers and an ifdef _WIN32 ... directive in test/sockets2.cpp would be the best way.

I could also add some public function, say create_listening_socket(), with two overloads that factors out the socket creation code in socket_kernel_1[2].cpp, and use that.

Let me know what you prefer, and thanks for reviewing!

@davisking

Copy link
Copy Markdown
Owner

Can't just do basically that in the tests or even better have an abstracted generic method for doing it?

Yes, it's easy enough to just create, bind, and listen on a socket to use for the test, but I wasn't sure if putting a bunch of platform specific headers and an ifdef _WIN32 ... directive in test/sockets2.cpp would be the best way.

I could also add some public function, say create_listening_socket(), with two overloads that factors out the socket creation code in socket_kernel_1[2].cpp, and use that.

Let me know what you prefer, and thanks for reviewing!

Yeah make some public function that cleanly does it and use that in the tests. That would be sweet.

@kSkip

kSkip commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

I believe this is ready now. I added a public function called create_listening_socket which returns an error code and writes to a socket type argument. That socket can then be used in create_listener_from_socket to produce equivalent behavior to create_listener.

This also allows for using sockets obtained from external APIs in listener objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants