Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed _pickle.UnpicklingError #576

Merged
merged 1 commit into from
Aug 31, 2024
Merged

fix: Fixed _pickle.UnpicklingError #576

merged 1 commit into from
Aug 31, 2024

Conversation

felipe3dfx
Copy link
Member

Error Details
The migration process attempted to apply the migration 0003_drop_pickle but failed with the following traceback:

Operations to perform:
  Target specific migration: 0003_drop_pickle, from constance
Running migrations:
  Applying constance.0003_drop_pickle...Traceback (most recent call last):
  File "/code/manage.py", line 23, in <module>
    main()
  File "/code/manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/code/.venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/code/.venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/code/.venv/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/code/.venv/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.12/site-packages/django/core/management/base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.12/site-packages/django/core/management/commands/migrate.py", line 357, in handle
    post_migrate_state = executor.migrate(
                         ^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
            ^^^^^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 255, in apply_migration
    state = migration.apply(state, schema_editor)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.12/site-packages/django/db/migrations/migration.py", line 132, in apply
    operation.database_forwards(
  File "/code/.venv/lib/python3.12/site-packages/django/db/migrations/operations/special.py", line 196, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/code/.venv/lib/python3.12/site-packages/constance/migrations/0003_drop_pickle.py", line 43, in migrate_pickled_data
    print(pickle.loads(value))
          ^^^^^^^^^^^^^^^^^^^
_pickle.UnpicklingError: invalid load key, '{'.

Error Details
The migration process attempted to apply the migration 0003_drop_pickle but failed with the following traceback:

```bash
Operations to perform:
  Target specific migration: 0003_drop_pickle, from constance
Running migrations:
  Applying constance.0003_drop_pickle...Traceback (most recent call last):
  File "/code/manage.py", line 23, in <module>
    main()
  File "/code/manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/code/.venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/code/.venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/code/.venv/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/code/.venv/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.12/site-packages/django/core/management/base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.12/site-packages/django/core/management/commands/migrate.py", line 357, in handle
    post_migrate_state = executor.migrate(
                         ^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
            ^^^^^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 255, in apply_migration
    state = migration.apply(state, schema_editor)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.12/site-packages/django/db/migrations/migration.py", line 132, in apply
    operation.database_forwards(
  File "/code/.venv/lib/python3.12/site-packages/django/db/migrations/operations/special.py", line 196, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/code/.venv/lib/python3.12/site-packages/constance/migrations/0003_drop_pickle.py", line 43, in migrate_pickled_data
    print(pickle.loads(value))
          ^^^^^^^^^^^^^^^^^^^
_pickle.UnpicklingError: invalid load key, '{'.
```

The error indicates that there is an issue with unpickling data during the migration process. Specifically, it raises an `_pickle.UnpicklingError` with the message "invalid load key, '{'." This suggests that the data being loaded may be corrupted or not in the expected format.
@camilonova camilonova merged commit cf838ab into master Aug 31, 2024
17 checks passed
@Mogost
Copy link
Member

Mogost commented Sep 1, 2024

Not sure that this is a right fix of the problem. This fix only ignores the problem. @camilonova please let a chance for review before merging

@Mogost
Copy link
Member

Mogost commented Sep 1, 2024

@felipe3dfx what value is stored in the problem key? I guess some collection list or dict?

@ivan-klass
Copy link
Contributor

ivan-klass commented Sep 3, 2024

This isn't a fix but a careless suppress of an underlying error. Chances are high that this can lead to much more serious troubles - users got exceptions much more later, when there's no clear context of the migration issues.

It would be better to understand the problem. The exception here seems like some kind of re-applying the migration, as { is faced where pickle data is expected.

picle.loads(b'{"__key__": "default", "__value__": 1}')
...
_pickle.UnpicklingError: invalid load key, '{'.

Was it a second attempt of the migration? If so, what was a first error?
Anyway, if reapplying the migration was the case, maybe better to check whether a value is already in a new format and only suppress in that specific case? That would be much more safe

@hvdklauw
Copy link

hvdklauw commented Sep 3, 2024

Can this be released please?

The issue happens when migrate fails after constance has done it's thing and redis contains the new data while the migration (when run again) still expects the old format.

@felipe3dfx
Copy link
Member Author

@Mogost The value that raised the issue was: b'{"__type__": "default", "__value__": "We are in a testing stage"}' and was raised when running all migrations of an application using Docker containers. In other words, when the database is empty and applying all migrations.

@Mogost
Copy link
Member

Mogost commented Sep 3, 2024

This fix should not be released in this form. I fully support @ivan-klass's

Anyway, if reapplying the migration was the case, maybe better to check whether a value is already in a new format and only suppress in that specific case? That would be much more safe

@ivan-klass
Copy link
Contributor

@hvdklauw thank you for the context. Not sure where redis get the value of new format, but if there's a rush on this, I would still recommend to limit the suppress to that case of re-applying the conversion

@ivan-klass
Copy link
Contributor

Working on the PR right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants