django-adminactions icon indicating copy to clipboard operation
django-adminactions copied to clipboard

create_extra_permissions crashes on occasion

Open timthelion opened this issue 4 years ago • 0 comments

There is an incredibly subtle bug with create_extra_permissions. Since the order of django signal handling is entirely arbitrary, sometimes you end up with the signals in this order.

(Pdb) self.receivers
[(('adminactions.create_extra_permissions', 139924311855616), <weakref at 0x7f42a474c360; to 'function' at 0x7f42a425c700 (create_extra_permissions_handler)>), ((139924257999456, 139924311855616), <weakref at 0x7f42a34be2c0; to 'function' at 0x7f42a7b18a60 (create_contenttypes)>), (('django.contrib.auth.management.create_permissions', 139924311855616), <weakref at 0x7f42a34be860; to 'function' at 0x7f42a7b28820 (create_permissions)>), ((139924254068592, 139924254551872), <weakref at 0x7f42a2f680e0; to 'function' at 0x7f42a7758f70 (create_default_site)>)]

This means that adminactions' create_extra_permissions function is called before django.contrib.auth.management.create_permissions and crashes with an exception like

psycopg2.errors.ForeignKeyViolation: insert or update on table "auth_permission" violates foreign key constraint "auth_permission_content_type_id_2f476e4b_fk_django_co"
DETAIL:  Key (content_type_id)=(505) is not present in table "django_content_type".

Luckily I have one test case locally where I can reproduce this reliably.

I'm working on a fix now.

This https://github.com/saxix/django-adminactions/pull/198 almost works.

But falls strangely short, recording all but 9 content types to the db.

(Pdb) len(content_types)
134
(Pdb) len(ContentType.objects.all())
125

timthelion avatar Dec 06 '21 20:12 timthelion