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

オープン
#639 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
57/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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
主要言語
Python
スター
458
フォーク
123
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

oracle/python-oracledb のほかの issue

oracle/python-oracledb の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。