Bulk edit overwrites Project changes made while waiting for a lock

Open
#112 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start by tracing the Projects bulk edit flow and ScriptProject.save(), especially where selected objects are loaded and locks are acquired. The report gives a reproducible PostgreSQL advisory lock sequence and confirms the same lost update for both name and comments. Done when bulk edits preserve concurrent changes to fields they do not modify and the reported reproduction no longer loses either update.

Written by the indexing model from the issue text.

Description

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

v0.0.1

NetBox Version

v4.7.4

Python Version

3.12

Steps to Reproduce
  1. Create two Script Projects named "Alpha Devices" and "Bravo Devices", leaving their comments empty.
  2. In manage.py nbshell, hold Alpha's Project lock. Keep the shell open:
    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. Select both Projects in the Projects list and apply a bulk edit that sets their description to "bulk". The request waits for Alpha's lock.
  4. While it waits, rename Bravo through PATCH /api/plugins/netbox-scripts/projects/<Bravo's id>/ with {"name": "Bravo Devices Renamed"}. Confirm that the PATCH succeeds.
  5. Release Alpha's lock in the shell:
    cursor.execute('SELECT pg_advisory_unlock(%s, %s)', key)
    cursor.close()
    
Expected Behavior

Both descriptions become "bulk", and Bravo keeps its new name, "Bravo Devices Renamed". Fields not changed by the bulk edit retain updates committed while it was waiting.

Observed Behavior

The bulk edit succeeds, but Bravo's name returns to "Bravo Devices".

The change log records the rename, followed by the bulk edit with "Bravo Devices" in both its before and after data. The overwrite therefore does not appear as a name change in that entry.

Repeating the test with {"comments": "Edited during the wait"} in step 4 also loses the concurrent update, restoring the previous empty comments.

The bulk edit loads the selected Projects before ScriptProject.save() acquires their locks. After waiting, it saves those previously loaded instances, including fields the bulk edit did not change.

Dominant language
Python
Stars
8
Forks
0
Avg merge
11h 41m
Merged PRs (30d)
38

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from netbox-community/netbox-scripts

All issues in netbox-community/netbox-scripts

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.