stencil buffer artifacts

I am trying to implement a “capping”-algorithm for FreeCAD. The code and the result can be seen in this topic here:
As you can see there are some problems with the implementation: Some pixels are not rendered properly. These problems occure, at positions where lines are in the background. I have tried many differet ways, but no success so far.
The opengl-code can be found in the stencil-buffer function. (python)
I am no familiar with opengl, so there is a good chance I have done something wrong.

Any hint/idea can help.
nice regards