cirosantilli / vcdvcd

Python Verilog value change dump (VCD) parser library + the nifty vcdcat VCD command line pretty printer.
Other
54 stars 21 forks source link

Handle broken pipes gracefully #35

Closed pcolladosoto closed 9 months ago

pcolladosoto commented 9 months ago

When piping the output of vcdcat to programs closing the reading end of the pipe before vcdcat finishes writing output the BrokenPipeError exception is not handled. Given vcdcat leverages the builtin print function throughout the implementation, we have installed the default handler for the SIGPIPE signal taking special care to avoid breaking compatibility with Windows (where SIGPIPE is not defined).

We have also checked the tests mentioned on README.md still pass.

Should another strategy be pursued please feel free to let us know so that we can take a look.

Thanks a ton for developing and extending vcdvcd! It's come in quite handy!

cirosantilli commented 9 months ago

Hello. Can you describe the use case a bit more? Generally killing a program when the pipe it writes to closes is a reasonable default.

pcolladosoto commented 9 months ago

Hi! I wrote a small script for parsing the output of vcdcat to analyse some waveforms. When developing the parser I used head(1) to get samples of what I would have to process, and that triggered the errors due to head(1)'s intended behaviour. On top of that, if I terminate the parser before it reads the entire output of vcdcat (say, for only parsing a small subset of the data) the error crops up back again.

Even though this isn't a deal breaker (it just appends some lines at the end of stderr), I thought it could be worthwhile to have vcdcat exit cleanly...

Anyway, if you'd like more details I can provide a sample parser.

Thanks a ton for making vcdvcd!

cirosantilli commented 9 months ago

OK, I'm starting to remember this stuff. A minimal reproduction is:

main.py

#!/usr/bin/env python
# import signal
# signal.signal(signal.SIGPIPE, signal.SIG_DFL)
i = 0
while True:
    print(i)
    i += 1

then:

main.py | head -n1

blows up with:

Traceback (most recent call last):
  File "/home/ciro/test/./main.py", line 6, in <module>
    print(i)
BrokenPipeError: [Errno 32] Broken pipe

related:

Let's merge this unless someday we find it causes some problem.

pcolladosoto commented 9 months ago

Yep! The example you provided is just what's going on... If anything comes up feel free to let me know so that I can lend a hand if necessary. Thanks a ton for your work again! All the best!