I am using the PUB/SUB socket pattern of ZeroMQ. The PUB produces stocks' financial data and publish them. The topic is set as each stock's code. At the SUB side, clients can subscribe for data which they want based on stock code. The PUB is written in C and the SUB is written in Python.
However, something goes wrong during testing. If only one stock code is set as a message filter on the SUB socket, everything works well. But when it comes to a large number of stocks, the program will crash in a short time, giving the error "Segmentation fault (core dumped)" ( details follow below ).
Here is the code of PUB (C) :
while (1) {
int rc = 0;
// send topic
rc = zmq_send(pub_socket, topic, rc, ZMQ_SNDMORE);
if (rc == -1) {
// error handling
}
// send stock data
rc = zmq_send(pub_socket, data, rc, 0);
if (rc == -1) {
// error handling
}
}
Here is the code of SUB (Python):
import zmq
# initialize a SUB socket
ctx = zmq.Context.instance()
socket = ctx.socket(zmq.SUB)
# set socket options to filter message
for code in code_list:
socket.setsockopt_string(zmq.SUBSCRIBE, code)
socket.connect(PUB_ADDR)
# recv data from PUB
while True:
data = socket.recv()
print(data)
I also used gdb to debug the program. The debug result is like:
debug result

Does anyone know why the program crashes? Any help is welcome, thanks.
Update:
If I replace the setsockopt_string part with the following code, the Python script works well. Strange thing... I need to dig deeper into the setsockopt_string function.
New code in Python:
socket.setsockopt_string( zmq.SUBSCRIBE, "" )
Latest Update:
I ran the script provided by @user3666197 and got a debug log. I select only several parts of the log because it's very long.
Socket initialization
setsockopt_string finished
recv one msg and exited
