How to Fix "Assertion !bs->started failed" in PyBGPStream


When using PyBGPStream for BGP data analysis, we can run into a small issue navigating the stream.

Suppose we execute the following code.

stream = pybgpstream.BGPStream(data_interface="singlefile")
for fpath in files:
  stream.set_data_interface_option("singlefile", "rib-file", fpath)
  for elem in stream:
    print(elem)

This will yield the following error:

python3: bgpstream.c:200: bgpstream_set_data_interface_option: 
Assertion `!bs->started' failed.
Aborted (core dumped)

In the error message, bs represents the stream itself. We are trying to set interface options (bgpstream_set_data_interface_option) on an already started stream, which we can’t do.

Unfortunately, we can’t reuse stream objects. Instead, we’ll close the stream when the object goes out of scope and create another one in the next iteration.

for fpath in files:
  stream = pybgpstream.BGPStream(data_interface="singlefile")
  stream.set_data_interface_option("singlefile", "rib-file", fpath)
  for elem in stream:
    print(elem)