Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault after running for a couple minutes #105

Closed
brandoningli opened this issue Jun 8, 2021 · 36 comments
Closed

Segmentation fault after running for a couple minutes #105

brandoningli opened this issue Jun 8, 2021 · 36 comments
Labels
bug Something isn't working

Comments

@brandoningli
Copy link
Contributor

brandoningli commented Jun 8, 2021

While running, I get an average of 19-20fps at 720p, which is much better than previous versions getting single-digit frame rates at that resolution.

However, after running for a minute or two, the program produces a Segmentation fault and stops. I'm able to restart the script and it picks right back up, but again Seg faults after a minute or two.

Pop!_OS (Ubuntu) 20.04
Kernel 5.11.0-7614-generic
Project just pulled minutes ago.
Python 3.8.5
v4l2loopback-dkms version 0.12.3-1ubuntu0.4 (from repos). I haven't yet tried compiling from source, but can if requested.
Pip Package versions:

numpy 1.20,3
opencv-python 4.5.2.54
requests 2.23.0
requests-unixsocket 0.2.0
pyfakewebcam 0.1.0
mediapipe 0.8.5
urllib3 1.25.8
idna 2.8
certifi 2019.11.28
chardet 3.0.4
six 1.15.0
opencv-contrib-python 4.5.2.54
absl-py 0.11.0
wheel 0.36.2
protobuf 3.14.0
attrs 20.3.0

Program output:

$ python3 fake.py  -w /dev/video2 -v /dev/video7
Real camera original values are set as: 640x480 with 30 FPS and video codec 1448695129
Real camera new values are set as: 1280x720 with 30 FPS and video codec 1196444237
Running...
Please CTRL-C to reload the background / foreground images
Please CTRL-\ to exit
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Segmentation fault (core dumped)
@fangfufu
Copy link
Owner

fangfufu commented Jun 8, 2021

Please try and run it in --akvcam mode. You don't have to have installed akvcam for that. Do let me know if that helps.

@brandoningli
Copy link
Contributor Author

brandoningli commented Jun 8, 2021

Running it with --akvcam causes weird image artifacting/duplication and an almost immediate segfault. akvcam is not installed on this machine.

@fangfufu
Copy link
Owner

fangfufu commented Jun 8, 2021

Okay, maybe compiling a new version of v4l2loopback-dkms, or try installing the actual akvcam. It does appear that running it with --akvcam causes the program to segfault.

@brandoningli
Copy link
Contributor Author

brandoningli commented Jun 8, 2021

Just built v4l2lopback following the instructions in the README, and I'm still getting segfaults.

I'll try giving akvcam an install.

@fangfufu
Copy link
Owner

fangfufu commented Jun 8, 2021

Okay, try the following:

  1. Make sure you unloaded the older version of v4l2lopback by rebooting
  2. Run fakecam.py without --akvcam
    If segfault still happens, do let me know which webcam software you tried to use before it segfaults.

Then do the followings:
3. Install akvcam, run fakecam without --akvcam
4. Run fakecam with --akvcam

Segfault has got more to do with the C library my Python code is calling, than my own Python code.

@brandoningli
Copy link
Contributor Author

brandoningli commented Jun 8, 2021

I've been using the Zoom desktop client and Jitsi Meet (via Firefox 89) to test (but not both programs at the same time).

ETA: It doesn't segfault when launching any program. It segfaults while using said program.

The results of doing the above:

  1. Segfault still happens
  2. Same behavior as without akvcam
  3. Giving no -v parameter to a v4l2loopback virtual camera,
$ python3 fake.py -w /dev/video2 --akvcam
Real camera original values are set as: 640x480 with 30 FPS and video codec 1448695129
Real camera new values are set as: 1280x720 with 30 FPS and video codec 1196444237
Traceback (most recent call last):
  File "fake.py", line 421, in <module>
    main()
  File "fake.py", line 393, in main
    cam = FakeCam(
  File "fake.py", line 155, in __init__
    self.fake_cam = AkvCameraWriter(v4l2loopback_path, self.width, self.height)
  File "/home/brandon/Documents/git/Linux-Fake-Background-Webcam/akvcam.py", line 11, in __init__
    self.d = self.open_camera()
  File "/home/brandon/Documents/git/Linux-Fake-Background-Webcam/akvcam.py", line 24, in open_camera
    ioctl(d, v4l2.VIDIOC_S_FMT, vid_format)
OSError: [Errno 22] Invalid argument
Exception ignored in: <function AkvCameraWriter.__del__ at 0x7ff99bd320d0>
Traceback (most recent call last):
  File "/home/brandon/Documents/git/Linux-Fake-Background-Webcam/akvcam.py", line 32, in __del__
    os.close(self.d)
AttributeError: 'AkvCameraWriter' object has no attribute 'd'

Giving a v4l2loopback virtual camera with -v, it's running, but the picture is showing weird artifacting (green and magenta, has horizontal bars, and is duplicated). It also segfaults.

I have an akvcam device with an informational graphic, but I don't know how to get the fake background webcam program to interface with it. I've never used this module before.

@brandoningli
Copy link
Contributor Author

brandoningli commented Jun 9, 2021

While this doesn't solve the segfault issue, I made a workaround to restart the script when it does. It produces a bit of a flash when the camera restarts (which can be quite frequently and very annoying), but otherwise starts back up very quickly and seems to work decently in the interim. Exiting with CTRL + \ produces a 0 exit code and this script ends gracefully.

#!/bin/bash
CODE=1
while [ "$CODE" -ne "0" ]; do
  python3 /path/to/fake.py -w /dev/video2 -v /dev/video7 -i /path/to/image/folder
  CODE=$?
done

@fangfufu fangfufu added bug Something isn't working help wanted Extra attention is needed labels Jun 9, 2021
@fangfufu
Copy link
Owner

fangfufu commented Jun 9, 2021

You need to specify -v with the akvcam device. akvcam adds two devices after your last webcam device. Whether or not you run it with AkvCameraWriter is sort of irrelevant at this point. We just want to stop the segfault.

@brandoningli
Copy link
Contributor Author

I can confirm that there's still a segfault running with akvcam and the akvcam device, even segfaulting faster on average than using v4l2loopback.

@fangfufu
Copy link
Owner

fangfufu commented Jun 9, 2021

What's your operating system?

@brandoningli
Copy link
Contributor Author

Pop!_OS 20.04 LTS. It's based on Ubuntu 20.04 LTS.

@fangfufu
Copy link
Owner

fangfufu commented Jun 9, 2021

In that case I have no idea then. I will leave this ticket open.

@fangfufu
Copy link
Owner

@brandoningli , please give the latest commit a go. Please reconfigure akvcam based on the README.

@brandoningli
Copy link
Contributor Author

When running fake.py, you need to set -v to the second video device that Akvcam created, e.g. if Akvcam created /dev/video5 and /dev/video6, you need to set -v /dev/video5.

Did you mean to say the first device or video6?

@fangfufu
Copy link
Owner

video 5, apologise. I will amend the README.

@brandoningli
Copy link
Contributor Author

Also, is --akvcam still required to be used? It's being reported as an unrecognized argument.

@fangfufu
Copy link
Owner

Nope, I completely removed that option.

@brandoningli
Copy link
Contributor Author

Ok. It's still in the README, but not in the usage info in the program.

@fangfufu
Copy link
Owner

Done, updated README.

@brandoningli
Copy link
Contributor Author

I'm still getting frequent segfaults, and my computer seems to crash completely 15-30 seconds afterwards. I have no way of directly blaming the script or akvcam, but it happens reliably after the segfault and rarely any other time.

@fangfufu
Copy link
Owner

Okay maybe try the Docker version? I suspect it is the difference between OS versions. It runs pretty stably on my Debian Stable

@brandoningli
Copy link
Contributor Author

Has the docker version been updated to use mediapipe? I've used the older docker version before with success, but poor performance.

@fangfufu
Copy link
Owner

Someone supposedly patched the Dockerfile, and if you re-generate the Docker image, it should be up-to-date.

@brandoningli
Copy link
Contributor Author

It has updated to the new mediapipe implementation, but there's two issues with the Docker implementation:

  1. The Dockerfile calls for copying akvcam.py which doesn't exist anymore. I can submit that in a PR shortly.
  2. It too exits with a code 139, which could indicate a segfault.

@fangfufu
Copy link
Owner

Okay, definitely submit your PR.

I think I am just going to leave this issue open. I can't suggest any ways for you to fix it. I don't know if vanilla Ubuntu 20.04 would have the same problem.

@brandoningli
Copy link
Contributor Author

Thanks for your help, though!

@fangfufu fangfufu removed the bug Something isn't working label Jun 11, 2021
@fangfufu
Copy link
Owner

@brandoningli , could you give this a go please on normal v4l2loopback-dkms please? I suspect this might be some sort of race condition going on.
https://github.com/fangfufu/Linux-Fake-Background-Webcam/tree/single-threaded

@brandoningli
Copy link
Contributor Author

brandoningli commented Jun 11, 2021

I don''t want to jinx it, but it's already been running longer without segfaulting than any other version. I'll leave it running in the background for a while and report back if it segfaults or keeps running. Performance seems on-par with the regular mediapipe version before it segfaulted as well.

@fangfufu fangfufu added bug Something isn't working and removed help wanted Extra attention is needed labels Jun 11, 2021
@fangfufu
Copy link
Owner

Basically I tried to add more multithreading, and I tried putting the mask generation within the RealCam class. I kept getting segfaults myself too. This led to the suspicion of race conditions. Race condition is not exactly easy to diagnose, especially I don't really use Python enough. I wonder if this race condition was introduced after I removed all the async stuff.

But if this patch works, then I will merge it to master.

@brandoningli
Copy link
Contributor Author

brandoningli commented Jun 11, 2021

Ah, race conditions. Gotta love them.

It's been running stably for me now for about ten minutes, which is much longer than before the patch. If it was going to segfault, it would've by now.

@brandoningli
Copy link
Contributor Author

Hold that thought.
When stopping, I got this error:

Traceback (most recent call last):
  File "fake.py", line 380, in <module>
    main()
  File "fake.py", line 377, in main
    cam.run()
  File "fake.py", line 286, in run
    frame = self.compose_frame(frame)
  File "fake.py", line 258, in compose_frame
    frame[:, :, c] = frame[:, :, c] * mask + background_frame[:, :, c] * (1 - mask)
  File "fake.py", line 344, in sigquit_handler
    cam.stop()
  File "fake.py", line 274, in stop
    self.real_cam.stop()
AttributeError: 'RealCam' object has no attribute 'stop'

@fangfufu
Copy link
Owner

That got patched in b80e1e9

@fangfufu
Copy link
Owner

Thank you so much for helping out!

@fangfufu
Copy link
Owner

@brandoningli , btw, if you need help with job applications / Masters applications / PhD course applications, feel free to ping me an email. I saw on your Github profile that you are open for work.

@fangfufu
Copy link
Owner

Also, what's your CPU, if you don't mind me asking.

@brandoningli
Copy link
Contributor Author

Intel i5-6300HQ (4) @ 3.200GHz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants