Use of cx_Oracle module appears to break subprocess module
I have encountered a bizarre bug. If I create an instance of cx_Oracle, from that point onwards, the returncode from a process created with subprocess.Popen will always be 0 even when the process exit code is non-zero!
The error is neither a hang nor a crash - it causes failed child processes to appear to succeed even when they fail.
The problem is reproducible 100% of the time using this simple script - the output follows:
#!/bin/python3
import sys
import platform
print("platform.platform:", platform.platform())
print("sys.maxsize > 2**32:", sys.maxsize > 2**32)
print("platform.python_version:", platform.python_version())
# Order of the following imports does not make any difference
import subprocess
import cx_Oracle
print("cx_Oracle.version:", cx_Oracle.version)
print("cx_Oracle.clientversion:", cx_Oracle.clientversion())
print('')
print('The following command will fail and a non-zero exit code is expected:')
rc = subprocess.Popen(('cat', 'no_such_file')).wait()
print(f'rc: expected: 1, actual: {rc}')
print('Invoking cx_Oracle - from this point onwards, ALL child processes will always return ')
print('a zero returncode (SUCCESS) even when the process returns a non-zero code!')
connection = cx_Oracle.Connection(mode = cx_Oracle.SYSDBA)
connection.close()
connection = None
print('The following command will fail so a non-zero exit code is expected,')
print('yet subprocess will now return 0:')
rc = subprocess.Popen(('cat', 'no_such_file')).wait()
print(f'rc: expected: 1, actual: {rc}')
The output:
platform.platform: Linux-3.10.0-957.el7.x86_64-x86_64-with-centos-7.6.1810-Core
sys.maxsize > 2**32: True
platform.python_version: 3.6.6
cx_Oracle.version: 7.2.1
cx_Oracle.clientversion: (18, 3, 0, 0, 0)
The following command will fail and a non-zero exit code is expected:
cat: no_such_file: No such file or directory
rc: expected: 1, actual: 1
Invoking cx_Oracle - from this point onwards, ALL child processes will always return
a zero returncode (SUCCESS) even when the process returns a non-zero code!
The following command will fail and a non-zero exit code is expected,
yet subprocess will now return 0:
cat: no_such_file: No such file or directory
rc: expected: 1, actual: 0
I just tried running your code on 64-bit Oracle Linux with Python 3.6 and do not see this behavior. That suggests this is a configuration issue, but what that is I don't know! Hopefully you can figure out what is causing the problem. Try with a newer version of the Oracle Client libraries. Try with no extra Oracle configuration, etc. And please do let us know if you discover the culprit! Thanks.
On a different server with a different OS (Centos7), but no Oracle installed, I installed the latest cx_Oracle 7.2.3 and Oracle instant client 19.5. I then changed the Connection args to something invalid (user = 'abc') (in a try: except: so that when it fails the 2nd subprocess can still run).
The 2nd subprocess returned 1 as expected. However I then simply defined ORACLE_SID=my_DB_name and ran my test script again. Of course the connect failed but the issue reproduced and subprocess returned 0!
I have no idea why that is happening that way. Perhaps @cjbj might have an idea?
Maybe play with Oracle signal handlers? DIAG_SIGHANDLER_ENABLED, BEQUEATH_DETACH and maybe more?
Hi, I'm having the exact same issue! I'm running the same test as the OP locally on a machine with Oracle 12EE (12.1.0.2.190416). So I'm not pointing to a instant client, but full db (with grid on its side). Database is local. The OS user running the python script is a superuser (sudoer) within the same OS groups of the oracle owner OS user. I'm running the test from a pipenv shell (see below the Pipfile content packages and requires).
Any advice?
platform.platform: Linux-3.10.0-957.12.2.el7.x86_64-x86_64-with-redhat-7.7-Maipo sys.maxsize > 2**32: True platform.python_version: 3.6.8 cx_Oracle.version: 7.3.0 cx_Oracle.clientversion: (12, 1, 0, 2, 0)
The following command will fail and a non-zero exit code is expected: cat: no_such_file: No such file or directory rc: expected: 1, actual: 1 Invoking cx_Oracle - from this point onwards, ALL child processes will always return a zero returncode (SUCCESS) even when the process returns a non-zero code! The following command will fail so a non-zero exit code is expected, yet subprocess will now return 0: cat: no_such_file: No such file or directory rc: expected: 1, actual: 0
Pipfile [packages] packaging = "" wget = "" paramiko = "" requests = "" cx_oracle = "*"
[requires] python_version = "3.6"
I did additional tests.
In the first test I've added the following to sqlnet.ora: DIAG_SIGHANDLER_ENABLED=FALSE DIAG_ADR_ENABLED=OFF DIAG_DDE_ENABLED=FALSE The results were the same as above (the issue persists).
In the second test, I've removed the DIAG* parameters from the sqlnet.ora and added the following: BEQUEATH_DETACH=YES The result was correct and issue disappeared.
Now question: How safe is to set BEQUEATH_DETACH=YES in sqlnet.ora on server side? Can it be fixed in a different way, like at specific session/connection level? Can this be considere a bug on cx_Oracle?
Regards,
denismonari and cjbj, I also tried setting BEQUEATH_DETACH=YES in sqlnet.ora and it resolved the issue for me. So now to investigate any side effects if any... So looking at https://www.orafaq.com/wiki/Sqlnet.ora#bequeath_detach I see this is a known issue.
Thanks @ambercap. That explanation you linked to makes sense. Whether the default should be BEQUEATH_DETACH=YES or whether there are any other side effects I don't know -- but I have that set in my own sqlnet.ora file -- which explains why I wasn't able to reproduce the issue! Perhaps @cjbj can shed additional light on this parameter, but I think its fairly clear at this point that it isn't something in cx_Oracle itself but the way the Oracle Client libraries have been designed to work.
Thanks @anthony-tuininga My point is, why using cx_Oracle cause mess on subprocess? I understand the BEQUEATH_DETACH drive some specific behaviour on Oracle client and the way you connect to database locally, but it should not change the behaviour of Python.
@denismonari, it affects the way the process handles subprocesses. So as soon as you load the Oracle client libraries with the default BEQUEATH_DETACH=no the way subprocesses are handled will immediately change. If you set BEQUEATH_DEATCH=yes, then the usual way subprocesses are handled is left unchanged. Again, I don't know the reason for why it does this by default, but the way to tell it to stop messing with your subprocess handling code is clear, I think. :-)
Thank again @anthony-tuininga and thank you for your patience. :-) I'm wondering how to make my program use cx_Oracle and not crash. It largely use suprocess to interact with other entities and I don't think I will be able to change sqlnet.ora just to place a workaround for cx_Oracle. It would require testing to check for behavioural changes.
Try it as an environment var export BEQUEATH_DETACH=YES
Is it a requirement to connect to the database via bequeath connection? If not you can connect to the database via the listener using TCP or IPC and not needing to change BEQUEATH_DETACH setting.
Hi all, and thank you again. Exporting BEQUEATH_DETACH=YES as an environment var is not working. Local connection is a requirement for some specific actions.
You can create a new sqlnet.ora file containing only the single line BEQUEATH_DETACH=YES in any directory you want such as /home/oracle. Then export TNS_ADMIN=/home/oracle before running your Python code. The sqlnet.ora in the location pointed to by the environment variable TNS_ADMIN will be picked up before the usual one in $ORACLE_HOME/network/admin
Closing - assuming issue has been resolved