A session that kills a busy one with ALTER SYSTEM KILL SESSION ... IMMEDIATE has its own connection closed (ORA-00031)

Abierto
#639 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
57/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python

Línea de trabajo

Start by reading the ORA-00031 entry in src/oracledb/errors.py and how _check_and_raise_exception() uses ERR_ORACLE_ERROR_XREF; the report says the same error is also listed in ODPI-C’s connection-closing errors. Reproduce with the supplied script in Thin and Thick modes, then check that the killer reports ORA-00031 while remaining healthy and can run another statement. The report does not name a test file.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug
  1. What versions are you using?
  • Oracle Database 23ai Free 23.26.3.0.0 (container image)
  • python-oracledb 26.1.0, Thin mode, and Thick mode with Oracle Instant Client 23.26.4.0.0: both behave the same way
  • Python 3.14.10, Linux x86_64, sys.maxsize > 2**32: True
  1. Is it an error or a hang or a crash?

An error: a healthy connection is closed by the driver.

  1. What error(s) or behavior you are seeing?

When a session kills another one that is busy in a call, with ALTER SYSTEM KILL SESSION '<sid>,<serial#>' IMMEDIATE,
the database answers the killer with ORA-00031 (session marked for kill): the target can't be ended at once, and
ends shortly after with ORA-00028. ORA-00031 is about the other session: the killer's own session is fine.

python-oracledb treats ORA-00031 as a dead session and closes the killer's connection:

killer: alter system kill session '58,49241' immediate
    -> DPY-4011: the database or network closed the connection / ORA-00031: session marked for kill
    is_healthy(): False
killer: select 'the killer still works' from dual
    -> DPY-1001: not connected to database
    is_healthy(): False

In Thick mode, the same, with DPI-1080: connection was closed by ORA-00031.

Expected: the killer gets ORA-00031 as an ordinary DatabaseError, and its connection remains usable. That's what
OCI does on its own: in a C program with OCI 23.26.4 (no ODPI-C), the killer gets ORA-00031,
OCI_ATTR_SERVER_STATUS stays OCI_SERVER_NORMAL, and its next statement works.

The cause seems to be the 31: ERR_CONNECTION_CLOSED entry in ERR_ORACLE_ERROR_XREF in
src/oracledb/errors.py, which makes _check_and_raise_exception() disconnect, and, in Thick mode, the same entry
in ODPI-C's list of errors that close the connection (DPI-1080). A session never gets ORA-00031 about itself
(killing the current session fails with ORA-00027), so it doesn't belong in these lists. With that line removed
from errors.py, Thin mode behaves as expected:

killer: alter system kill session '58,...' immediate
    -> ORA-00031: session marked for kill
    is_healthy(): True
victim: begin dbms_session.sleep(10); end;
    -> DPY-4011: the database or network closed the connection / ORA-00028: session has been terminated / ...
    is_healthy(): False
killer: select 'the killer still works' from dual
    -> ok ('the killer still works',)

Applications that kill sessions (administration tools, watchdogs that end runaway queries) lose their own
connection, its transaction and its session state whenever the target is busy, which is the usual reason to kill
it; a pooled connection is dropped, and killing a second session on the same connection fails with DPY-1001. The
DPY-4011 message also suggests a network problem. Without IMMEDIATE, the killer gets no error (the database waits
for the target to end), so it isn't affected.

  1. Does your application call init_oracle_client()?

Both: Thin mode (no) and Thick mode (yes) are affected. The script below uses Thick mode when given an Instant Client
directory.

  1. Include a runnable Python script that shows the problem.

The user needs the ALTER SYSTEM privilege. No tables are needed.

"""
A session that kills a busy one with ALTER SYSTEM KILL SESSION ... IMMEDIATE gets ORA-00031 (session marked for
kill), and python-oracledb then closes the killer's own connection. Self-contained: python-oracledb only.

Usage: python repro_kill_session.py user password host:port/service [instant client lib dir: Thick mode]
The user needs the ALTER SYSTEM privilege. The session's serial number comes from DBMS_DEBUG_JDWP, which needs no
access to V$SESSION.
"""

import sys
import threading
import time

import oracledb

user, password, dsn = sys.argv[1:4]
if len(sys.argv) > 4:
    oracledb.init_oracle_client(lib_dir=sys.argv[4])


def run(name, conn, sql):
    try:
        with conn.cursor() as cursor:
            cursor.execute(sql)
            row = cursor.fetchone() if cursor.description else None
        outcome = f"ok {row}"
    except oracledb.Error as e:
        outcome = " / ".join(line for line in str(e).splitlines() if not line.startswith("Help:"))
    print(f"{name}: {sql}\n    -> {outcome}\n    is_healthy(): {conn.is_healthy()}")


killer = oracledb.connect(user=user, password=password, dsn=dsn)
victim = oracledb.connect(user=user, password=password, dsn=dsn)
print(f"python-oracledb {oracledb.__version__}, thin={killer.thin}, database {killer.version}\n")

with victim.cursor() as cursor:
    cursor.execute("select dbms_debug_jdwp.current_session_id || ',' || "
                   "dbms_debug_jdwp.current_session_serial from dual")
    victim_session = cursor.fetchone()[0]

# The victim is busy for 10 seconds; the killer kills it after half a second
killer_thread = threading.Thread(
    target=lambda: (time.sleep(0.5),
                    run("killer", killer, f"alter system kill session '{victim_session}' immediate"))
)
killer_thread.start()
run("victim", victim, "begin dbms_session.sleep(10); end;")
killer_thread.join()

# The killer's session is fine, but its connection is gone
run("killer", killer, "select 'the killer still works' from dual")

Output (Thin mode):

python-oracledb 26.1.0, thin=True, database 23.26.3.0.0

victim: begin dbms_session.sleep(10); end;
    -> DPY-4011: the database or network closed the connection / ORA-00028: session has been terminated / ORA-06512: at "SYS.DBMS_SESSION", line 438 / ORA-06512: at line 1
    is_healthy(): False
killer: alter system kill session '58,49241' immediate
    -> DPY-4011: the database or network closed the connection / ORA-00031: session marked for kill
    is_healthy(): False
killer: select 'the killer still works' from dual
    -> DPY-1001: not connected to database
    is_healthy(): False

Output (Thick mode):

python-oracledb 26.1.0, thin=False, database 23.26.3.0.0

victim: begin dbms_session.sleep(10); end;
    -> DPY-4011: the database or network closed the connection / DPI-1080: connection was closed by ORA-00028 / ORA-00028: session has been terminated / ORA-06512: at "SYS.DBMS_SESSION", line 438 / ORA-06512: at line 1
    is_healthy(): False
killer: alter system kill session '58,18807' immediate
    -> DPY-4011: the database or network closed the connection / DPI-1080: connection was closed by ORA-00031 / ORA-00031: session marked for kill
    is_healthy(): False
killer: select 'the killer still works' from dual
    -> DPY-1001: not connected to database / DPI-1010: not connected
    is_healthy(): False
Lenguaje dominante
Python
Estrellas
458
Forks
123
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de oracle/python-oracledb

Todos los issues de oracle/python-oracledb

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.