When updating user password or creating a new user pre_save and post_save signals are getting triggered twice #10000

Closed
opened 2025-12-29 21:25:28 +01:00 by adam · 4 comments
Owner

Originally created by @nishant131 on GitHub (Jul 23, 2024).

Deployment Type

Self-hosted

NetBox Version

v4.0.7

Python Version

3.10

Steps to Reproduce

  1. Add a signal post_save and/ or pre_save in the netbox/users/signals.py file. Refer the below example.
from django.db.models.signals import post_save

@receiver(post_save, sender=User)
def post_create_update_user_signal(sender, instance, created, update_fields=None, **kwargs):
    logger = logging.getLogger('users.signals')
    logger.debug("post_save signal triggered for user: %s", instance.username)
    pass
  1. Make sure you set appropriate logging in the netbox/netbox/configuration.py file. Refer below example.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'users.signals': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        }
    },
}
  1. Login as a superuser.
  2. Click "Admin" -> "Authentication" -> "Users"
  3. Click on "+ Add".
  4. Fill the details for the user and make sure to setup a password.
  5. Click "Create".
  6. Observe the logs. Logs should contain the following lines (twice):
post_save signal triggered for user: username
post_save signal triggered for user: username

Instead of adding a new user from step 5 to 7, we can also just update the password and first name for any of the existing users. The result would be the same.

Expected Behavior

post_save user signal should be called only once.

Observed Behavior

post_save user signal called twice.

Originally created by @nishant131 on GitHub (Jul 23, 2024). ### Deployment Type Self-hosted ### NetBox Version v4.0.7 ### Python Version 3.10 ### Steps to Reproduce 1. Add a signal `post_save` and/ or `pre_save` in the `netbox/users/signals.py` file. Refer the below example. ``` from django.db.models.signals import post_save @receiver(post_save, sender=User) def post_create_update_user_signal(sender, instance, created, update_fields=None, **kwargs): logger = logging.getLogger('users.signals') logger.debug("post_save signal triggered for user: %s", instance.username) pass ``` 2. Make sure you set appropriate logging in the `netbox/netbox/configuration.py` file. Refer below example. ``` LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', }, }, 'loggers': { 'users.signals': { 'handlers': ['console'], 'level': 'DEBUG', 'propagate': False, } }, } ``` 3. Login as a superuser. 4. Click "Admin" -> "Authentication" -> "Users" 5. Click on "+ Add". 6. Fill the details for the user and make sure to setup a password. 7. Click "Create". 8. Observe the logs. Logs should contain the following lines (twice): ``` post_save signal triggered for user: username post_save signal triggered for user: username ``` Instead of adding a new user from step 5 to 7, we can also just update the password and first name for any of the existing users. The result would be the same. ### Expected Behavior `post_save` user signal should be called only once. ### Observed Behavior `post_save` user signal called twice.
adam added the type: bug label 2025-12-29 21:25:28 +01:00
adam closed this issue 2025-12-29 21:25:28 +01:00
Author
Owner

@nishant131 commented on GitHub (Jul 23, 2024):

The root cause for this issue is the save() method of UserForm class in the netbox/users/forms/model_forms.py#L215. It calls the super().save(*args, **kwargs) which internally calls self.instance.save() and when the password is set it again calls instance.save(). Hence, saving the object twice and triggering the signal twice.

@nishant131 commented on GitHub (Jul 23, 2024): The root cause for this issue is the `save()` method of `UserForm` class in the [netbox/users/forms/model_forms.py#L215](https://github.com/netbox-community/netbox/blob/develop/netbox/users/forms/model_forms.py#L215). It calls the `super().save(*args, **kwargs)` which internally calls `self.instance.save()` and when the password is set it again calls `instance.save()`. Hence, saving the object twice and triggering the signal twice.
Author
Owner

@jeffgdotorg commented on GitHub (Jul 24, 2024):

Thanks for reporting this problem.

I've been unable to reproduce it in the lab on a pristine v4.0.7 system; perhaps I've missed a step? I added the signal receiver as directed, and I'm using the following LOGGING config:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/opt/netbox/logs/netbox.log',
        },
    },
    'loggers': {
        'users.signals': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': False,
        }
    },
}

Filesystem permissions are correct, I've run upgrade.sh and restarted the services, and NetBox was able to create the named file, but adding a user or changing an existing user's password and first name does not result in anything being logged there:

ubuntu@nb407-repro-16962:/opt/netbox$ ls -l logs/
total 0
-rw-r--r-- 1 netbox netbox 0 Jul 24 13:52 netbox.log
ubuntu@nb407-repro-16962:/opt/netbox$ cat logs/netbox.log
ubuntu@nb407-repro-16962:/opt/netbox$
@jeffgdotorg commented on GitHub (Jul 24, 2024): Thanks for reporting this problem. I've been unable to reproduce it in the lab on a pristine v4.0.7 system; perhaps I've missed a step? I added the signal receiver as directed, and I'm using the following `LOGGING` config: ```python LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'file': { 'level': 'DEBUG', 'class': 'logging.FileHandler', 'filename': '/opt/netbox/logs/netbox.log', }, }, 'loggers': { 'users.signals': { 'handlers': ['file'], 'level': 'DEBUG', 'propagate': False, } }, } ``` Filesystem permissions are correct, I've run `upgrade.sh` and restarted the services, and NetBox was able to create the named file, but adding a user or changing an existing user's password and first name does not result in anything being logged there: ``` ubuntu@nb407-repro-16962:/opt/netbox$ ls -l logs/ total 0 -rw-r--r-- 1 netbox netbox 0 Jul 24 13:52 netbox.log ubuntu@nb407-repro-16962:/opt/netbox$ cat logs/netbox.log ubuntu@nb407-repro-16962:/opt/netbox$ ```
Author
Owner

@jeffgdotorg commented on GitHub (Jul 24, 2024):

After discussing this issue in our team standup, I'm resolving it as invalid. I understand that your signal addition is necessary to illustrate the problem, but one of our intake standards is that the bug must be reproducible in a "vanilla" installation of NetBox, which this one by definition is not.

If you're able to trigger the problem in a plugin, please create a new report and we will evaluate it anew.

@jeffgdotorg commented on GitHub (Jul 24, 2024): After discussing this issue in our team standup, I'm resolving it as invalid. I understand that your signal addition is necessary to illustrate the problem, but one of our intake standards is that the bug must be reproducible in a "vanilla" installation of NetBox, which this one by definition is not. If you're able to trigger the problem in a plugin, please create a new report and we will evaluate it anew.
Author
Owner

@nishant131 commented on GitHub (Jul 24, 2024):

@jeffgdotorg,
I changed the LOGGING configuration with the one that you have mentioned, but I am able to reproduce it:

netbox@37dee6eb55ab:/opt/netbox$ ls -l logs/netbox.log 
-rw-r--r-- 1 root root 104 Jul 24 15:02 logs/netbox.log
netbox@37dee6eb55ab:/opt/netbox$ cat logs/netbox.log 
post_save signal triggered for user: new_user@gmail.com
post_save signal triggered for user: new_user@gmail.com

If it helps, I have dockerized the code, so environment might not be exactly the same.

Although, you have mentioned the filesystem permissions are correct. I see that you are logged in as ubuntu user but the owner and group of the netbox.log file shows netbox and netbox. I might be missing something over here. Can you try changing the permissions to 666?

If above does not work, can you try adding following lines to netbox/users/forms/model_forms.py#L215?

        import logging
        logger = logging.getLogger('users.signals')
        logger.debug('saving userform')

If the above line does not come up in the netbox.log file then, there might be some issue with LOGGING configuration, otherwise there could be some issue with configuring post_save signal.

@nishant131 commented on GitHub (Jul 24, 2024): @jeffgdotorg, I changed the `LOGGING` configuration with the one that you have mentioned, but I am able to reproduce it: ``` netbox@37dee6eb55ab:/opt/netbox$ ls -l logs/netbox.log -rw-r--r-- 1 root root 104 Jul 24 15:02 logs/netbox.log netbox@37dee6eb55ab:/opt/netbox$ cat logs/netbox.log post_save signal triggered for user: new_user@gmail.com post_save signal triggered for user: new_user@gmail.com ``` If it helps, I have dockerized the code, so environment might not be exactly the same. Although, you have mentioned the filesystem permissions are correct. I see that you are logged in as `ubuntu` user but the owner and group of the `netbox.log` file shows netbox and netbox. I might be missing something over here. Can you try changing the permissions to 666? If above does not work, can you try adding following lines to [netbox/users/forms/model_forms.py#L215](https://github.com/netbox-community/netbox/blob/develop/netbox/users/forms/model_forms.py#L215)? ``` import logging logger = logging.getLogger('users.signals') logger.debug('saving userform') ``` If the above line does not come up in the `netbox.log` file then, there might be some issue with `LOGGING` configuration, otherwise there could be some issue with configuring post_save signal.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#10000