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

[13.0] MemoryError in website_sale post-migration with many images #4712

Open
huguesdk opened this issue Jan 20, 2025 · 3 comments
Open

[13.0] MemoryError in website_sale post-migration with many images #4712

huguesdk opened this issue Jan 20, 2025 · 3 comments
Labels

Comments

@huguesdk
Copy link
Member

huguesdk commented Jan 20, 2025

module

website_sale

describe the bug

when running a migration from 12.0 to 13.0 with many product.image records, the migration fails after displaying many MemoryError errors.

to reproduce

affected versions: upgrade to 13.0

steps to reproduce the behavior:

  1. have a database in version 12.0 with website_sale installed and having many product.image records (in my case, 21k).
  2. run openupgrade to upgrade to 13.0.

expected behavior
the database and its filestore should be correctly upgraded to 13.0.

additional context

log:

2025-01-15 17:21:08,640 1 INFO odoo-upgrade odoo.modules.migration: module website_sale: Running migration [13.0.1.0>] post-migration 
2025-01-15 17:21:08,643 1 INFO odoo-upgrade OpenUpgrade: website_sale: post-migration script called with version 12.0.1.0 
2025-01-15 17:21:08,643 1 INFO odoo-upgrade odoo.models: Computing parent_path for table product_public_category... 
2025-01-15 17:21:08,659 1 DEBUG odoo-upgrade OpenUpgrade: 0 rows affected after 0:00:00.003386 running 
        UPDATE product_image
        SET name = 'Image ' || id
        WHERE name IS NULL  
2025-01-15 17:23:14,217 1 ERROR odoo-upgrade post-migration: Error while recovering product.image>image for 19497: MemoryError() 
2025-01-15 17:23:14,234 1 ERROR odoo-upgrade post-migration: Error while recovering product.image>image for 19496: MemoryError() 
2025-01-15 17:23:14,235 1 ERROR odoo-upgrade post-migration: Error while recovering product.image>image for 19495: MemoryError() 
2025-01-15 17:23:14,236 1 ERROR odoo-upgrade post-migration: Error while recovering product.image>image for 19494: MemoryError() 
2025-01-15 17:23:14,238 1 ERROR odoo-upgrade post-migration: Error while recovering product.image>image for 19493: MemoryError() 
2
(…)
2025-01-15 17:23:20,981 1 ERROR odoo-upgrade post-migration: Error while recovering product.image>image for 15232: MemoryError() 
2025-01-15 17:23:20,982 1 ERROR odoo-upgrade post-migration: Error while recovering product.image>image for 15231: MemoryError() 
2025-01-15 17:23:20,983 1 ERROR odoo-upgrade post-migration: Error while recovering product.image>image for 15230: MemoryError() 
2025-01-15 17:23:21,010 1 ERROR odoo-upgrade OpenUpgrade: website_sale: error in migration script /odoo_env/src/openupgrade/addons/website_sale/migrations/13.0.1.0/post-migration.py:  
2025-01-15 17:23:21,010 1 ERROR odoo-upgrade OpenUpgrade:  
Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/api.py", line 748, in get
    value = self._data[field][record._ids[0]]
KeyError: 139810

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1038, in __get__
    value = env.cache.get(record, self)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 754, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('ir.attachment(139810,).datas', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/api.py", line 748, in get
    value = self._data[field][record._ids[0]]
KeyError: 139810

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1038, in __get__
    value = env.cache.get(record, self)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 754, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('ir.attachment(139810,).store_fname', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1147, in compute_value
    records._compute_field_value(self)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 4021, in _compute_field_value
    getattr(self, field.compute)()
  File "/odoo_env/src/openupgrade/odoo/addons/base/models/ir_attachment.py", line 199, in _compute_datas
    if attach.store_fname:
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1045, in __get__
    recs._fetch_field(self)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 3006, in _fetch_field
    self._read(fnames)
  File "/odoo_env/src/openupgrade/odoo/addons/base/models/ir_attachment.py", line 486, in _read
    return super(IrAttachment, self)._read(fields)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 3099, in _read
    self.env.cache.update(fetched, field, values)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 773, in update
    self._data[field].update(zip(records._ids, values))
MemoryError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/addons/website_sale/migrations/13.0.1.0/post-migration.py", line 32, in convert_image_attachments
    Model.browse(attachment.res_id).image_1920 = attachment.datas
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1062, in __get__
    self.compute_value(recs)
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 2001, in compute_value
    super().compute_value(records)
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1150, in compute_value
    env.add_to_compute(field, records)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 690, in add_to_compute
    self.all.tocompute[field].update(records._ids)
MemoryError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/api.py", line 748, in get
    value = self._data[field][record._ids[0]]
KeyError: 139810

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1038, in __get__
    value = env.cache.get(record, self)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 754, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('ir.attachment(139810,).res_id', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/venv/lib/python3.7/site-packages/openupgradelib/openupgrade.py", line 2285, in wrapped_function
    version,
  File "/odoo_env/src/openupgrade/addons/website_sale/migrations/13.0.1.0/post-migration.py", line 66, in migrate
    convert_image_attachments(env)
  File "/odoo_env/src/openupgrade/addons/website_sale/migrations/13.0.1.0/post-migration.py", line 38, in convert_image_attachments
    attachment.res_id,
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1045, in __get__
    recs._fetch_field(self)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 3006, in _fetch_field
    self._read(fnames)
  File "/odoo_env/src/openupgrade/odoo/addons/base/models/ir_attachment.py", line 486, in _read
    return super(IrAttachment, self)._read(fields)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 3099, in _read
    self.env.cache.update(fetched, field, values)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 773, in update
    self._data[field].update(zip(records._ids, values))
MemoryError
2025-01-15 17:23:21,082 1 ERROR odoo-upgrade odoo.modules.loading: Error executing post migration script for module website_sale
`-> website_sale_management
`-> website_sale_product_description
`-> website_sale_product_sort
`-> website_sale_product_style_badge
`-> website_sale_require_legal
:  
2025-01-15 17:23:21,086 1 ERROR odoo-upgrade odoo.modules.registry: Failed to load registry 
Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/api.py", line 748, in get
    value = self._data[field][record._ids[0]]
KeyError: 139810

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1038, in __get__
    value = env.cache.get(record, self)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 754, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('ir.attachment(139810,).datas', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/api.py", line 748, in get
    value = self._data[field][record._ids[0]]
KeyError: 139810

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1038, in __get__
    value = env.cache.get(record, self)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 754, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('ir.attachment(139810,).store_fname', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1147, in compute_value
    records._compute_field_value(self)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 4021, in _compute_field_value
    getattr(self, field.compute)()
  File "/odoo_env/src/openupgrade/odoo/addons/base/models/ir_attachment.py", line 199, in _compute_datas
    if attach.store_fname:
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1045, in __get__
    recs._fetch_field(self)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 3006, in _fetch_field
    self._read(fnames)
  File "/odoo_env/src/openupgrade/odoo/addons/base/models/ir_attachment.py", line 486, in _read
    return super(IrAttachment, self)._read(fields)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 3099, in _read
    self.env.cache.update(fetched, field, values)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 773, in update
    self._data[field].update(zip(records._ids, values))
MemoryError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/addons/website_sale/migrations/13.0.1.0/post-migration.py", line 32, in convert_image_attachments
    Model.browse(attachment.res_id).image_1920 = attachment.datas
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1062, in __get__
    self.compute_value(recs)
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 2001, in compute_value
    super().compute_value(records)
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1150, in compute_value
    env.add_to_compute(field, records)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 690, in add_to_compute
    self.all.tocompute[field].update(records._ids)
MemoryError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/api.py", line 748, in get
    value = self._data[field][record._ids[0]]
KeyError: 139810

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1038, in __get__
    value = env.cache.get(record, self)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 754, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('ir.attachment(139810,).res_id', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/modules/registry.py", line 87, in new
    odoo.modules.load_modules(registry._db, force_demo, status, update_module)
  File "/odoo_env/src/openupgrade/odoo/modules/loading.py", line 489, in load_modules
    force, status, report, loaded_modules, update_module, models_to_check, upg_registry)
  File "/odoo_env/src/openupgrade/odoo/modules/loading.py", line 368, in load_marked_modules
    upg_registry=upg_registry,
  File "/odoo_env/src/openupgrade/odoo/modules/loading.py", line 256, in load_module_graph
    migrations.migrate_module(package, 'post')
  File "/odoo_env/src/openupgrade/odoo/modules/migration.py", line 191, in migrate_module
    migrate(self.cr, installed_version)
  File "/opt/venv/lib/python3.7/site-packages/openupgradelib/openupgrade.py", line 2285, in wrapped_function
    version,
  File "/odoo_env/src/openupgrade/addons/website_sale/migrations/13.0.1.0/post-migration.py", line 66, in migrate
    convert_image_attachments(env)
  File "/odoo_env/src/openupgrade/addons/website_sale/migrations/13.0.1.0/post-migration.py", line 38, in convert_image_attachments
    attachment.res_id,
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1045, in __get__
    recs._fetch_field(self)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 3006, in _fetch_field
    self._read(fnames)
  File "/odoo_env/src/openupgrade/odoo/addons/base/models/ir_attachment.py", line 486, in _read
    return super(IrAttachment, self)._read(fields)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 3099, in _read
    self.env.cache.update(fetched, field, values)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 773, in update
    self._data[field].update(zip(records._ids, values))
MemoryError
2025-01-15 17:23:21,087 1 CRITICAL odoo-upgrade odoo.service.server: Failed to initialize database `odoo-upgrade`. 
Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/api.py", line 748, in get
    value = self._data[field][record._ids[0]]
KeyError: 139810

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1038, in __get__
    value = env.cache.get(record, self)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 754, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('ir.attachment(139810,).datas', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/api.py", line 748, in get
    value = self._data[field][record._ids[0]]
KeyError: 139810

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1038, in __get__
    value = env.cache.get(record, self)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 754, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('ir.attachment(139810,).store_fname', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1147, in compute_value
    records._compute_field_value(self)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 4021, in _compute_field_value
    getattr(self, field.compute)()
  File "/odoo_env/src/openupgrade/odoo/addons/base/models/ir_attachment.py", line 199, in _compute_datas
    if attach.store_fname:
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1045, in __get__
    recs._fetch_field(self)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 3006, in _fetch_field
    self._read(fnames)
  File "/odoo_env/src/openupgrade/odoo/addons/base/models/ir_attachment.py", line 486, in _read
    return super(IrAttachment, self)._read(fields)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 3099, in _read
    self.env.cache.update(fetched, field, values)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 773, in update
    self._data[field].update(zip(records._ids, values))
MemoryError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/addons/website_sale/migrations/13.0.1.0/post-migration.py", line 32, in convert_image_attachments
    Model.browse(attachment.res_id).image_1920 = attachment.datas
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1062, in __get__
    self.compute_value(recs)
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 2001, in compute_value
    super().compute_value(records)
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1150, in compute_value
    env.add_to_compute(field, records)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 690, in add_to_compute
    self.all.tocompute[field].update(records._ids)
MemoryError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/api.py", line 748, in get
    value = self._data[field][record._ids[0]]
KeyError: 139810

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1038, in __get__
    value = env.cache.get(record, self)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 754, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('ir.attachment(139810,).res_id', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/odoo_env/src/openupgrade/odoo/service/server.py", line 1194, in preload_registries
    registry = Registry.new(dbname, update_module=update_module)
  File "/odoo_env/src/openupgrade/odoo/modules/registry.py", line 87, in new
    odoo.modules.load_modules(registry._db, force_demo, status, update_module)
  File "/odoo_env/src/openupgrade/odoo/modules/loading.py", line 489, in load_modules
    force, status, report, loaded_modules, update_module, models_to_check, upg_registry)
  File "/odoo_env/src/openupgrade/odoo/modules/loading.py", line 368, in load_marked_modules
    upg_registry=upg_registry,
  File "/odoo_env/src/openupgrade/odoo/modules/loading.py", line 256, in load_module_graph
    migrations.migrate_module(package, 'post')
  File "/odoo_env/src/openupgrade/odoo/modules/migration.py", line 191, in migrate_module
    migrate(self.cr, installed_version)
  File "/opt/venv/lib/python3.7/site-packages/openupgradelib/openupgrade.py", line 2285, in wrapped_function
    version,
  File "/odoo_env/src/openupgrade/addons/website_sale/migrations/13.0.1.0/post-migration.py", line 66, in migrate
    convert_image_attachments(env)
  File "/odoo_env/src/openupgrade/addons/website_sale/migrations/13.0.1.0/post-migration.py", line 38, in convert_image_attachments
    attachment.res_id,
  File "/odoo_env/src/openupgrade/odoo/fields.py", line 1045, in __get__
    recs._fetch_field(self)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 3006, in _fetch_field
    self._read(fnames)
  File "/odoo_env/src/openupgrade/odoo/addons/base/models/ir_attachment.py", line 486, in _read
    return super(IrAttachment, self)._read(fields)
  File "/odoo_env/src/openupgrade/odoo/models.py", line 3099, in _read
    self.env.cache.update(fetched, field, values)
  File "/odoo_env/src/openupgrade/odoo/api.py", line 773, in update
    self._data[field].update(zip(records._ids, values))
MemoryError
2025-01-15 17:23:21,090 1 INFO odoo-upgrade odoo.service.server: Initiating shutdown 
2025-01-15 17:23:21,090 1 INFO odoo-upgrade odoo.service.server: Hit CTRL-C again or send a second signal to force the shutdown. 
2025-01-15 17:23:21,090 1 INFO odoo-upgrade odoo.sql_db: ConnectionPool(used=0/count=0/max=64): Closed 1 connections  

i tried using openupgrade.chunked() to iterate over the attachments. it doesn’t prevent the MemoryError errors, but it stops trying later.

@huguesdk huguesdk added the bug label Jan 20, 2025
@pedrobaeza
Copy link
Member

I don't think this is a question of the number of images, but a corrupted image that triggers this problem. Please investigate which one is triggering the error.

@huguesdk
Copy link
Member Author

@pedrobaeza thank you for your answer. i looked into it, but i could not (yet) find a corrupted image triggering the problem. at each run, the id of the product.image attachement that triggers the first error is different (sometimes higher, sometimes lower), so i still think it is a memory exhaustion problem due to the number of images.

we can see here that the function loops over all the product images and assigns attachment.datas to product.image.image_1920. is this actually loading the data from the files into a bytes string? at which point is this persisted to the filestore and removed from memory? i guess this only happens at the end of the transaction, right? there are tens of gigabytes of (base64-encoded) data handled in this loop, and the computer on which this runs has 32 gib of ram.

how can we force the data to be written to the filestore and removed from memory, or avoid loading so much data in memory? should we create the attachment and copy the file data by hand? i tried iterating over the ids, loading only one record at the time and committing the transaction at each step, but that didn’t change much.

@pedrobaeza
Copy link
Member

I see... then it's not that. I have migrated DBs with 30k products with images without problems in a 64 GB server, but I haven't dug in the internals on how the memory is managed in these cases. Maybe a savepoint/flush in between may help. chunked is not doing that.

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

No branches or pull requests

2 participants