Parallel Region Create API calls can produce top-level regions with the same tree_id #7940

Closed
opened 2025-12-29 20:30:17 +01:00 by adam · 7 comments
Owner

Originally created by @DevOpsFu on GitHub (Apr 24, 2023).

Originally assigned to: @arthanson on GitHub.

NetBox version

v3.4.8

Python version

3.8

Steps to Reproduce

Ref: https://github.com/e-breuninger/terraform-provider-netbox/issues/385

Using a tool which will produce parallel API calls to NetBox (e.g. Terraform), create two or more top-level Region objects with children, via the /dcim/regions endpoint.

In Terraform, this might look like this:

resource "netbox_region" "uk" {
  name = "United Kingdom"
}

resource "netbox_region" "ireland" {
  name = "Ireland"
}

resource "netbox_region" "london" {
  name             = "London"
  parent_region_id = netbox_region.uk.id
}

resource "netbox_region" "gatwick" {
  name             = "Gatwick"
  parent_region_id = netbox_region.uk.id
}



resource "netbox_region" "northeurope" {
  name             = "Azure North Europe"
  parent_region_id = netbox_region.ireland.id
}

resource "netbox_region" "uksouth" {
  name             = "Azure UK South"
  parent_region_id = netbox_region.london.id
}

Expected Behavior

The Regions are created with the correct tree structure in the NetBox UI

Observed Behavior

The top-level regions can sometimes be created with the same tree_id in the dcim_region table. This causes some weird UI issues whereby all the child regions will appear under one of the top-level regions, despite it not being their parent:

image

image

If you follow the child region links, the Parent IDs are set correctly.

To work around this, we can ensure that the top-level regions are created in serial so that they never receive the same tree_id value.

Originally created by @DevOpsFu on GitHub (Apr 24, 2023). Originally assigned to: @arthanson on GitHub. ### NetBox version v3.4.8 ### Python version 3.8 ### Steps to Reproduce Ref: https://github.com/e-breuninger/terraform-provider-netbox/issues/385 Using a tool which will produce parallel API calls to NetBox (e.g. Terraform), create two or more top-level Region objects with children, via the `/dcim/regions` endpoint. In Terraform, this might look like this: ``` resource "netbox_region" "uk" { name = "United Kingdom" } resource "netbox_region" "ireland" { name = "Ireland" } resource "netbox_region" "london" { name = "London" parent_region_id = netbox_region.uk.id } resource "netbox_region" "gatwick" { name = "Gatwick" parent_region_id = netbox_region.uk.id } resource "netbox_region" "northeurope" { name = "Azure North Europe" parent_region_id = netbox_region.ireland.id } resource "netbox_region" "uksouth" { name = "Azure UK South" parent_region_id = netbox_region.london.id } ``` ### Expected Behavior The Regions are created with the correct tree structure in the NetBox UI ### Observed Behavior The top-level regions can sometimes be created with the same `tree_id` in the `dcim_region` table. This causes some weird UI issues whereby all the child regions will appear under one of the top-level regions, despite it not being their parent: ![image](https://user-images.githubusercontent.com/7556656/234105395-23601149-d685-4a6d-82fe-7dcb05e80497.png) ![image](https://user-images.githubusercontent.com/7556656/234105426-273acbfe-08e5-4664-83a4-668ad2f2b8b0.png) If you follow the child region links, the Parent IDs are set correctly. To work around this, we can ensure that the top-level regions are created in serial so that they never receive the same `tree_id` value.
adam added the type: bugstatus: acceptedseverity: medium labels 2025-12-29 20:30:17 +01:00
adam closed this issue 2025-12-29 20:30:17 +01:00
Author
Owner

@arthanson commented on GitHub (Apr 26, 2023):

This looks like it is due to https://github.com/django-mptt/django-mptt/pull/677 see: https://github.com/django-mptt/django-mptt/issues/555 but the PR is stagnant so marking this as blocked for either #6587 or #11421

@arthanson commented on GitHub (Apr 26, 2023): This looks like it is due to https://github.com/django-mptt/django-mptt/pull/677 see: https://github.com/django-mptt/django-mptt/issues/555 but the PR is stagnant so marking this as blocked for either #6587 or #11421
Author
Owner

@arthanson commented on GitHub (Oct 2, 2023):

@DevOpsFu could you please test against https://github.com/netbox-community/netbox/pull/13942 I made the region API calls atomic which I think should fix the issue. I don't have terraform setup so don't have a good test scenario.

@arthanson commented on GitHub (Oct 2, 2023): @DevOpsFu could you please test against https://github.com/netbox-community/netbox/pull/13942 I made the region API calls atomic which I think should fix the issue. I don't have terraform setup so don't have a good test scenario.
Author
Owner

@DevOpsFu commented on GitHub (Oct 2, 2023):

@DevOpsFu could you please test against #13942 I made the region API calls atomic which I think should fix the issue. I don't have terraform setup so don't have a good test scenario.

I'd be happy to test! I'll see if I can get the code at that PR built in Docker and get back to you soon 👍

@DevOpsFu commented on GitHub (Oct 2, 2023): > @DevOpsFu could you please test against #13942 I made the region API calls atomic which I think should fix the issue. I don't have terraform setup so don't have a good test scenario. I'd be happy to test! I'll see if I can get the code at that PR built in Docker and get back to you soon 👍
Author
Owner

@DevOpsFu commented on GitHub (Oct 3, 2023):

Hi @arthanson , I have tested this against your branch but unfortunately I still get the same behaviour. The netbox stack was totally clean to start with, and running against a docker image built from your branch:

❯ docker ps
CONTAINER ID   IMAGE                                               COMMAND                  CREATED         STATUS                   PORTS                                       NAMES
e7dc6c711d55   netboxcommunity/netbox:12336-serialize-region-api   "/usr/bin/tini -- /o…"   5 minutes ago   Up 2 minutes (healthy)                                               netbox-docker-netbox-housekeeping-1
e2d05181e486   netboxcommunity/netbox:12336-serialize-region-api   "/usr/bin/tini -- /o…"   5 minutes ago   Up 2 minutes (healthy)                                               netbox-docker-netbox-worker-1
cf382b1dc07d   netboxcommunity/netbox:12336-serialize-region-api   "/usr/bin/tini -- /o…"   5 minutes ago   Up 5 minutes (healthy)   0.0.0.0:8000->8080/tcp, :::8000->8080/tcp   netbox-docker-netbox-1
7d36a9501ebc   postgres:15-alpine                                  "docker-entrypoint.s…"   5 minutes ago   Up 5 minutes             5432/tcp                                    netbox-docker-postgres-1
5676b041e21b   redis:7-alpine                                      "docker-entrypoint.s…"   5 minutes ago   Up 5 minutes             6379/tcp                                    netbox-docker-redis-cache-1
96767ca07745   redis:7-alpine                                      "docker-entrypoint.s…"   5 minutes ago   Up 5 minutes             6379/tcp                                    netbox-docker-redis-1

When running the Terraform code I pasted originally, I see the following region structure gets produced:

image

@DevOpsFu commented on GitHub (Oct 3, 2023): Hi @arthanson , I have tested this against your branch but unfortunately I still get the same behaviour. The netbox stack was totally clean to start with, and running against a docker image built from your branch: ``` ❯ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e7dc6c711d55 netboxcommunity/netbox:12336-serialize-region-api "/usr/bin/tini -- /o…" 5 minutes ago Up 2 minutes (healthy) netbox-docker-netbox-housekeeping-1 e2d05181e486 netboxcommunity/netbox:12336-serialize-region-api "/usr/bin/tini -- /o…" 5 minutes ago Up 2 minutes (healthy) netbox-docker-netbox-worker-1 cf382b1dc07d netboxcommunity/netbox:12336-serialize-region-api "/usr/bin/tini -- /o…" 5 minutes ago Up 5 minutes (healthy) 0.0.0.0:8000->8080/tcp, :::8000->8080/tcp netbox-docker-netbox-1 7d36a9501ebc postgres:15-alpine "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 5432/tcp netbox-docker-postgres-1 5676b041e21b redis:7-alpine "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 6379/tcp netbox-docker-redis-cache-1 96767ca07745 redis:7-alpine "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 6379/tcp netbox-docker-redis-1 ``` When running the Terraform code I pasted originally, I see the following region structure gets produced: ![image](https://github.com/netbox-community/netbox/assets/7556656/ed67e392-673a-4eb4-87c4-95f1f8d90938)
Author
Owner

@arthanson commented on GitHub (Oct 3, 2023):

Thanks @DevOpsFu I guess I will have to change to use locks, will get a new version out shortly.

@arthanson commented on GitHub (Oct 3, 2023): Thanks @DevOpsFu I guess I will have to change to use locks, will get a new version out shortly.
Author
Owner

@arthanson commented on GitHub (Oct 5, 2023):

@DevOpsFu could you please try one more time, I've updated the PR to use postgres locks which I hope will fix it.

@arthanson commented on GitHub (Oct 5, 2023): @DevOpsFu could you please try one more time, I've updated the PR to use postgres locks which I hope will fix it.
Author
Owner

@DevOpsFu commented on GitHub (Oct 12, 2023):

@arthanson Looks good! I did a few create/destroy cycles with the above TF code and the region tree looked correct every time. Thanks for looking at this!

@DevOpsFu commented on GitHub (Oct 12, 2023): @arthanson Looks good! I did a few create/destroy cycles with the above TF code and the region tree looked correct every time. Thanks for looking at this!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7940