Available Prefix/IP Advisory lock - Locking other endpoints #4722

Closed
opened 2025-12-29 19:19:52 +01:00 by adam · 2 comments
Owner

Originally created by @anttof on GitHub (Apr 6, 2021).

NetBox version

v2.10.8

Python version

3.8

Steps to Reproduce

  1. Create a prefix object (parameters doesn't matter, use netmask 29)

  2. Run following python code to interact with REST API: (replace variables)

import json 
import time
import requests 

NETBOX_API_KEY = "INSERT_API_TOKEN"
NETBOX_URL = "INSERT_API_URL"
PREFIX = "INSERT_PREFIX_OBJECT" # 1562

API_HEADERS = {
        'Authorization': f'Token {NETBOX_API_KEY}',
        'Accept': 'application/json',
        'Content-Type': 'application/json'}

try:
    allocated_ip = requests.post(f"{NETBOX_URL}/api/ipam/prefixes/{PREFIX}/available-ips/", headers=API_HEADERS).json()
except:
    pass

#time.sleep(10) # This seems to fix the issue. 
# Error occurs here when trying to do any action (getting devices/prefixes or anything else)
prefixes = requests.get(f"{NETBOX_URL}/api/ipam/prefixes/", headers=API_HEADERS).json()
# ^ This could be api/dcim/devices or any other endpoint, the result is the same. 

Expected Behavior

When receiving a response from the POST "available-ip" or "available-prefix" endpoint (whether prefix is full or not shouldn't matter) I should be able to access other endpoints as normal (for example GET /api/dcim/devices/) without receiving Exception in relation to previous request.

Observed Behavior

A JSONDecodeError is raised when trying to access REST API endpoints instantly after having used the POST available-ip/available-prefix endpoint. Implementing a sleep of 5-10 seconds between the two requests solves the issue.

Traceback (most recent call last):
  File "netbox_advisory_lock_bug.py", line 21, in <module>
    prefixes = requests.get(f"{NETBOX_URL}/api/ipam/prefixes/", headers=API_HEADERS).json()
  File "/usr/lib/python3/dist-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

This is a follow up on the following pynetbox issue, please see: https://github.com/digitalocean/pynetbox/issues/338#issuecomment-801012510

I am not 100% sure but from reading the documentation I'm guessing it might have something to do with the advisory lock feature that is used on the available-ip/available-prefix POST endpoints.

Originally created by @anttof on GitHub (Apr 6, 2021). ### NetBox version v2.10.8 ### Python version 3.8 ### Steps to Reproduce 1. Create a prefix object (parameters doesn't matter, use netmask 29) 2. Run following python code to interact with REST API: (replace variables) ``` import json import time import requests NETBOX_API_KEY = "INSERT_API_TOKEN" NETBOX_URL = "INSERT_API_URL" PREFIX = "INSERT_PREFIX_OBJECT" # 1562 API_HEADERS = { 'Authorization': f'Token {NETBOX_API_KEY}', 'Accept': 'application/json', 'Content-Type': 'application/json'} try: allocated_ip = requests.post(f"{NETBOX_URL}/api/ipam/prefixes/{PREFIX}/available-ips/", headers=API_HEADERS).json() except: pass #time.sleep(10) # This seems to fix the issue. # Error occurs here when trying to do any action (getting devices/prefixes or anything else) prefixes = requests.get(f"{NETBOX_URL}/api/ipam/prefixes/", headers=API_HEADERS).json() # ^ This could be api/dcim/devices or any other endpoint, the result is the same. ``` ### Expected Behavior When receiving a response from the POST "available-ip" or "available-prefix" endpoint (whether prefix is full or not shouldn't matter) I should be able to access other endpoints as normal (for example GET /api/dcim/devices/) without receiving Exception in relation to previous request. ### Observed Behavior A JSONDecodeError is raised when trying to access REST API endpoints instantly after having used the POST available-ip/available-prefix endpoint. Implementing a sleep of 5-10 seconds between the two requests solves the issue. ``` Traceback (most recent call last): File "netbox_advisory_lock_bug.py", line 21, in <module> prefixes = requests.get(f"{NETBOX_URL}/api/ipam/prefixes/", headers=API_HEADERS).json() File "/usr/lib/python3/dist-packages/requests/models.py", line 897, in json return complexjson.loads(self.text, **kwargs) File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads return _default_decoder.decode(s) File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode obj, end = self.raw_decode(s) File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode return self.scan_once(s, idx=_w(s, idx).end()) simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0) ``` This is a follow up on the following pynetbox issue, please see: https://github.com/digitalocean/pynetbox/issues/338#issuecomment-801012510 I am not 100% sure but from reading the documentation I'm guessing it might have something to do with the advisory lock feature that is used on the available-ip/available-prefix POST endpoints.
adam added the type: bugpending closure labels 2025-12-29 19:19:52 +01:00
adam closed this issue 2025-12-29 19:19:52 +01:00
Author
Owner

@github-actions[bot] commented on GitHub (Jun 6, 2021):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our contributing guide.

@github-actions[bot] commented on GitHub (Jun 6, 2021): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@github-actions[bot] commented on GitHub (Jul 7, 2021):

This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.

@github-actions[bot] commented on GitHub (Jul 7, 2021): This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4722