Concurrent bulk writes can deadlock when acquiring Project locks

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

メンテナーはふだん 1 日以内に返信

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

評価

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

調査の方向性

Start by tracing Project lock acquisition for REST bulk updates, UI bulk edits, imports, and bulk deletion; the issue names no source files or tests. Compare how each path orders locks, especially when a Project is renamed while requests overlap. Done means all these paths use a common immutable lock order and the reproduction completes without a deadlock.

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

説明

app: scripts Bucket: KTLO severity: low status: backlog type: bug
Plugin Version

v0.0.1

NetBox Version

v4.7.4

Python Version

3.12

Steps to Reproduce

Run the requests from separate clients so they can overlap.

  1. Create two Script Projects named Alpha Devices and Bravo Devices, with unique project keys. Note their IDs.
  2. In manage.py nbshell, hold Alpha's advisory lock. Keep the shell open until the final step.
    from django.db import connection
    from netbox_scripts.models import ScriptProject
    from netbox_scripts.storage.locks import advisory_key
    
    project = ScriptProject.objects.get(name='Alpha Devices')
    key = advisory_key(project.storage_key)
    cursor = connection.cursor()
    cursor.execute('SELECT pg_advisory_lock(%s, %s)', key)
    
  3. Send a bulk PATCH to /api/plugins/netbox-scripts/projects/, replacing the placeholders with the Projects' IDs:
    [
      {"id": <alpha_id>, "description": "first"},
      {"id": <bravo_id>, "description": "first"}
    ]
    
    Leave this request running while it waits for Alpha's lock.
  4. In another client, rename Bravo Devices to Aardvark Devices.
  5. Send another bulk update for the same two IDs, changing the description to "second". Leave this request running too.
  6. Once both requests are waiting, release Alpha's lock in the shell:
    cursor.execute('SELECT pg_advisory_unlock(%s, %s)', key)
    cursor.close()
    
Expected Behavior

Both requests complete without deadlocking. Concurrent bulk writes acquire Project locks in a consistent order, regardless of Project names.

Observed Behavior

One request fails with HTTP 500 and OperationalError: deadlock detected. The other request succeeds.

The first request collects its locks in name order, with Alpha before Bravo. After the rename, the second request takes Aardvark's lock before waiting for Alpha. When the shell releases Alpha's lock, each request can hold the lock the other needs.

The same failure was reproduced with two UI bulk edits and with a UI bulk edit overlapping a REST bulk update.

This is the follow-up discussed in #104. The fix needs a common immutable lock order across REST bulk updates, UI bulk edits and imports, and Project bulk deletion.

主要言語
Python
スター
8
フォーク
0
平均マージ
11時間 22分
マージ済み PR(30日)
40

環境構築

はじめの一歩

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

netbox-community/netbox-scripts のほかの issue

netbox-community/netbox-scripts の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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