Hi,
I tried to couple a (pre)forking socket server with foundationDB, using the C API directly.
The socket server is a slight modification of https://gist.github.com/paulsmith/204301
for unix domain sockets.
A client connects with an input that ultimately translates into a key to be looked up in a foundationDB database.
Using a variation of function getKey in
/*
* performance_test.c
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
This file has been truncated. show original
I intended to send the value corresponding to the key back to the client.
Everything works as intended when I disable forking alltogether.
However, with fork() enabled in the server code, the execution blocks indefinitely at the first fdb_future_block_until_ready invocation.
Questions: Is this expected behaviour? What are best practices to provide a concurrent socket interface to foundationDB using the low-level C API?
This would be easier to answer if you posted your code. Iām going to guess that the problem is that FoundationDB starts up a background thread which runs significant pieces of the client code, and multiple threads and fork do not play together well.
If you flip your code to spawning a new thread for each connection instead of forking, do things then work?
Thanks for the hints!
Yes, indeed the problem was about the mixing of fork (my code) and thread creation (in foundationDB init).
Cf. https://thorstenball.com/blog/2014/10/13/why-threads-cant-fork/ for a brief discussion.
The solution in my case was to init foundationDB late, i.e. in each fork()ed child.
1 Like