Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

500 errors with "duplicate key value" on remote_media_cache_thumbnails when dynamic_thumbnails enabled #2182

Closed

Description

@turt2live

Error is at the end of this issue (it's a long one).

The following URLs produce similar errors to the one included here:

There may be more, however those 3 are easily noticed.

Changing the method to scale does not result in an error. This does not appear to affect local media.

I tried to delete the files and records from the database in varying combinations (delete files & delete records, only delete records, only delete files) in various running states of synapse (running, shut down, restart after). I tried various combinations of deleting data from the two tables (from both remote_media_cache and remote_media_cache_thumbnails, only _thumbnails, only _cache). No luck.

The error continues and does not go away. If I'm missing a step, please let me know. Otherwise, this appears to be a very specific and strange bug.

Synapse version: v0.20.0

2017-05-02 20:35:23,067 - synapse.access.http.8008 - 59 - INFO - GET-753- <IP REDACTED> - 8008 - Received request: GET /_matrix/media/v1/thumbnail/matrix.org/ARfYZMTdGlRCAGXurhYSZGwT?width=128&height=128&method=crop
2017-05-02 20:35:23,068 - synapse.util.async - 201 - INFO - GET-753- Acquired linearizer lock 'media_remote' for key ('matrix.org', 'ARfYZMTdGlRCAGXurhYSZGwT')
2017-05-02 20:35:23,079 - synapse.util.async - 208 - INFO - GET-753- Releasing linearizer lock 'media_remote' for key ('matrix.org', 'ARfYZMTdGlRCAGXurhYSZGwT')
2017-05-02 20:35:23,083 - synapse.metrics - 162 - INFO - - Collecting gc 0
2017-05-02 20:35:23,106 - synapse.rest.media.v1.thumbnailer - 93 - INFO - - Stored thumbnail in file u'/root/synapse/media_store/remote_thumbnail/matrix.org/Sg/El/RAnfzhTBOJHxWANIWpBP/128-128-image-png'
2017-05-02 20:35:23,118 - synapse.metrics - 162 - INFO - - Collecting gc 0
2017-05-02 20:35:23,120 - synapse.http.server - 139 - ERROR - GET-753- Failed handle request synapse.rest.media.v1.thumbnail_resource._async_render_GET on <synapse.rest.media.v1.thumbnail_resource.ThumbnailResource instance at 0x7f3e179dabd8>: <XForwardedForRequest at 0x7f3dcf04b6c8 method=GET uri=/_matrix/media/v1/thumbnail/matrix.org/ARfYZMTdGlRCAGXurhYSZGwT?width=128&height=128&method=crop clientproto=HTTP/1.0 site=8008>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/synapse/http/server.py", line 118, in wrapped_request_handler
    yield request_handler(self, request)
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1299, in _inlineCallbacks
    result = result.throwExceptionIntoGenerator(g)
  File "/usr/local/lib/python2.7/dist-packages/twisted/python/failure.py", line 393, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/usr/local/lib/python2.7/dist-packages/synapse/rest/media/v1/thumbnail_resource.py", line 71, in _async_render_GET
    width, height, method, m_type
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1299, in _inlineCallbacks
    result = result.throwExceptionIntoGenerator(g)
  File "/usr/local/lib/python2.7/dist-packages/twisted/python/failure.py", line 393, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/usr/local/lib/python2.7/dist-packages/synapse/rest/media/v1/thumbnail_resource.py", line 194, in _select_or_generate_remote_thumbnail
    desired_height, desired_method, desired_type
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1299, in _inlineCallbacks
    result = result.throwExceptionIntoGenerator(g)
  File "/usr/local/lib/python2.7/dist-packages/twisted/python/failure.py", line 393, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/usr/local/lib/python2.7/dist-packages/synapse/rest/media/v1/media_repository.py", line 318, in generate_remote_exact_thumbnail
    t_width, t_height, t_type, t_method, t_len
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1299, in _inlineCallbacks
    result = result.throwExceptionIntoGenerator(g)
  File "/usr/local/lib/python2.7/dist-packages/twisted/python/failure.py", line 393, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/usr/local/lib/python2.7/dist-packages/synapse/storage/_base.py", line 406, in _simple_insert
    self._simple_insert_txn, table, values,
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1299, in _inlineCallbacks
    result = result.throwExceptionIntoGenerator(g)
  File "/usr/local/lib/python2.7/dist-packages/twisted/python/failure.py", line 393, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/usr/local/lib/python2.7/dist-packages/synapse/storage/_base.py", line 319, in runInteraction
    inner_func, *args, **kwargs
  File "/usr/local/lib/python2.7/dist-packages/twisted/python/threadpool.py", line 250, in inContext
    result = inContext.theWork()
  File "/usr/local/lib/python2.7/dist-packages/twisted/python/threadpool.py", line 266, in <lambda>
    inContext.theWork = lambda: context.call(ctx, func, *args, **kw)
  File "/usr/local/lib/python2.7/dist-packages/twisted/python/context.py", line 122, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/local/lib/python2.7/dist-packages/twisted/python/context.py", line 85, in callWithContext
    return func(*args,**kw)
  File "/usr/local/lib/python2.7/dist-packages/twisted/enterprise/adbapi.py", line 307, in _runWithConnection
    compat.reraise(excValue, excTraceback)
  File "/usr/local/lib/python2.7/dist-packages/twisted/enterprise/adbapi.py", line 298, in _runWithConnection
    result = func(conn, *args, **kw)
  File "/usr/local/lib/python2.7/dist-packages/synapse/storage/_base.py", line 313, in inner_func
    func, *args, **kwargs
  File "/usr/local/lib/python2.7/dist-packages/synapse/storage/_base.py", line 242, in _new_transaction
    r = func(txn, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/synapse/storage/_base.py", line 426, in _simple_insert_txn
    txn.execute(sql, vals)
  File "/usr/local/lib/python2.7/dist-packages/synapse/storage/_base.py", line 80, in execute
    self._do_execute(self.txn.execute, sql, *args)
  File "/usr/local/lib/python2.7/dist-packages/synapse/storage/_base.py", line 110, in _do_execute
    sql, *args
IntegrityError: duplicate key value violates unique constraint "remote_media_cache_thumbnails_media_origin_media_id_thumbna_key"
DETAIL:  Key (media_origin, media_id, thumbnail_width, thumbnail_height, thumbnail_type)=(matrix.org, ARfYZMTdGlRCAGXurhYSZGwT, 128, 128, image/png) already exists.

Activity

  1. jkolo commented on Jul 17, 2017

    @jkolo
    Contributor

    This is the reason I made pull request #1946

  2. Half-Shot commented on Apr 4, 2018

    @Half-Shot
    Collaborator

    This continues to bite me all the time on riot-android and the toasts that keep popping up are getting annoying.

  3. TildeSlashC0re commented on Apr 12, 2018

    @TildeSlashC0re

    I can confirm that I experience the same bug!

    Seems to only apply to specific remote thumnails.

    EDIT:

    DELETE FROM remote_media_cache_thumbnails;

    during stopped state, then restarting did the trick for me!

  4. Half-Shot commented on Jun 1, 2018

    @Half-Shot
    Collaborator

    Still experiencing this. It's a pretty awful experience for some avatars to load and some to fail.

  5. changed the title [-]Cannot get cropped thumbnail for some media[/-] [+]Cannot generate thumbnails for some media[/+] on Jun 13, 2018
  6. tleydxdy commented on Jun 13, 2018

    @tleydxdy

    Same in 0.31.1

  7. ananace commented on Jun 29, 2018

    @ananace

    The issue can be solved without doing full table migrations - at least on PostgreSQL, technically without even requiring downtime.

    ALTER TABLE remote_media_cache_thumbnails DROP CONSTRAINT remote_media_cache_thumbnails_media_origin_media_id_thumbna_key;
    CREATE INDEX remote_media_cache_thumbnails_media_origin_media_id_thumbna_key ON remote_media_cache_thumbnails (media_origin, media_id, thumbnail_width, thumbnail_height, thumbnail_type, thumbnail_method);
  8. zzottel commented on Jul 5, 2018

    @zzottel

    Just had the same after enabling dynamic thumbnails on my Synapse.

    Can confirm that the SQL posted by ananace helps, but what is the consequence? That now multiple thumbs of the same size are allowed, right?

    But why does Synapse even try to create its own dynamically if there's already one present for the desired resolution?

  9. TildeSlashC0re commented on Sep 8, 2018

    @TildeSlashC0re

    Dropping by to confirm that @ananace workaround did the trick!

  10. console-cowboy commented on Sep 20, 2018

    @console-cowboy

    This remains an issue as of the 0.33.4 Release. The @ananace workaround provides the relief others have mentioned though.

  11. ArchangeGabriel commented on Feb 6, 2019

    @ArchangeGabriel
    Contributor

    Could some dev comment on @ananace workaround consequences? Should we just do that?

  12. msrd0 commented on Feb 24, 2019

    @msrd0

    Still an issue on 0.99.1.1, @ananace's workaround still works. Would be nice to know how that affects my server though. Does that mean I store a new thumbnail every time a client asks for one?

  13. richvdh commented on Mar 22, 2019

    @richvdh
    Member

    Another report of this at #4915. Logs are:

    2019-03-22 10:42:58,815 - synapse.access.http.8008 - 233 - INFO - GET-3590- 131.246.146.10 - 8008 - Received request: GET /_matrix/media/r0/thumbnail/t2bot.io/193408b58f5e1eb72d9bea13f23914e6?width=120&height=120&method=scale&animated=true
    2019-03-22 10:42:58,816 - synapse.util.async_helpers - 205 - DEBUG - GET-3590- Acquired uncontended linearizer lock 'media_remote' for key ('t2bot.io', '193408b58f5e1eb72d9bea13f23914e6')
    2019-03-22 10:42:58,817 - synapse.storage.txn - 317 - DEBUG - GET-3590- [TXN START] {get_cached_remote_media-764a}
    2019-03-22 10:42:58,818 - synapse.storage.txn - 373 - DEBUG - GET-3590- [TXN END] {get_cached_remote_media-764a} 0.001629 sec
    2019-03-22 10:42:58,822 - synapse.util.async_helpers - 218 - DEBUG - GET-3590- Releasing linearizer lock 'media_remote' for key ('t2bot.io', '193408b58f5e1eb72d9bea13f23914e6')
    2019-03-22 10:42:58,822 - synapse.storage.txn - 317 - DEBUG - GET-3590- [TXN START] {get_remote_media_thumbnails-764b}
    2019-03-22 10:42:58,823 - synapse.storage.txn - 373 - DEBUG - GET-3590- [TXN END] {get_remote_media_thumbnails-764b} 0.000840 sec
    2019-03-22 10:42:58,832 - synapse.rest.media.v1.thumbnail_resource - 216 - DEBUG - GET-3590- We don't have a thumbnail of that size. Generating
    2019-03-22 10:42:59,012 - synapse.rest.media.v1.media_repository - 534 - INFO - GET-3590- Stored thumbnail in file '/var/lib/matrix-synapse/media/remote_thumbnail/t2bot.io/19/34/08b58f5e1eb72d9bea13f23914e6/120-120-image-png'
    2019-03-22 10:42:59,013 - synapse.storage.txn - 317 - DEBUG - GET-3590- [TXN START] {store_remote_media_thumbnail-764c}
    2019-03-22 10:42:59,015 - synapse.storage._base - 152 - DEBUG - GET-3590- [SQL FAIL] {store_remote_media_thumbnail-764c} duplicate key value violates unique constraint "remote_media_cache_thumbnails_media_origin_media_id_thumbna_key"
    DETAIL:  Key (media_origin, media_id, thumbnail_width, thumbnail_height, thumbnail_type)=(t2bot.io, 193408b58f5e1eb72d9bea13f23914e6, 120, 120, image/png) already exists.
    
    2019-03-22 10:42:59,015 - synapse.storage._base - 365 - DEBUG - GET-3590- [TXN FAIL] {store_remote_media_thumbnail-764c} duplicate key value violates unique constraint "remote_media_cache_thumbnails_media_origin_media_id_thumbna_key"
    DETAIL:  Key (media_origin, media_id, thumbnail_width, thumbnail_height, thumbnail_type)=(t2bot.io, 193408b58f5e1eb72d9bea13f23914e6, 120, 120, image/png) already exists.
    
    2019-03-22 10:42:59,015 - synapse.storage.txn - 373 - DEBUG - GET-3590- [TXN END] {store_remote_media_thumbnail-764c} 0.001923 sec
    2019-03-22 10:42:59,016 - synapse.http.server - 112 - ERROR - GET-3590- Failed handle request via 'ThumbnailResource': <XForwardedForRequest at 0x7f5490213d68 method='GET' uri='/_matrix/media/r0/thumbnail/t2bot.io/193408b58f5e1eb72d9bea13f23914e6?width=120&height=120&method=scale&animated=true' clientproto='HTTP/1.1' site=8008>
    Traceback (most recent call last):
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/synapse/http/server.py", line 81, in wrapped_request_handler yield h(self, request)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks result = result.throwExceptionIntoGenerator(g)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator return g.throw(self.type, self.value, self.tb)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/synapse/rest/media/v1/thumbnail_resource.py", line 78, in _async_render_GET width, height, method, m_type
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks result = result.throwExceptionIntoGenerator(g)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator return g.throw(self.type, self.value, self.tb)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/synapse/rest/media/v1/thumbnail_resource.py", line 221, in _select_or_generate_remote_thumbnail desired_height, desired_method, desired_type
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks result = result.throwExceptionIntoGenerator(g)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator return g.throw(self.type, self.value, self.tb)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/synapse/rest/media/v1/media_repository.py", line 540, in generate_remote_exact_thumbnail t_width, t_height, t_type, t_method, t_len
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks result = result.throwExceptionIntoGenerator(g)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator return g.throw(self.type, self.value, self.tb)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/synapse/storage/_base.py", line 516, in _simple_insert self._simple_insert_txn, table, values,
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks result = result.throwExceptionIntoGenerator(g)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator return g.throw(self.type, self.value, self.tb)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/synapse/storage/_base.py", line 408, in runInteraction *args, **kwargs
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks result = result.throwExceptionIntoGenerator(g)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator return g.throw(self.type, self.value, self.tb)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/synapse/storage/_base.py", line 457, in runWithConnection inner_func, *args, **kwargs
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/python/threadpool.py", line 250, in inContext result = inContext.theWork()
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/python/threadpool.py", line 266, in <lambda> inContext.theWork = lambda: context.call(ctx, func, *args, **kw)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/python/context.py", line 122, in callWithContext return self.currentContext().callWithContext(ctx, func, *args, **kw)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/python/context.py", line 85, in callWithContext return func(*args,**kw)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/enterprise/adbapi.py", line 306, in _runWithConnection compat.reraise(excValue, excTraceback)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/python/compat.py", line 464, in reraise raise exception.with_traceback(traceback)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/twisted/enterprise/adbapi.py", line 297, in _runWithConnection result = func(conn, *args, **kw)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/synapse/storage/_base.py", line 453, in inner_func return func(conn, *args, **kwargs)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/synapse/storage/_base.py", line 329, in _new_transaction r = func(txn, *args, **kwargs)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/synapse/storage/_base.py", line 536, in _simple_insert_txn txn.execute(sql, vals)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/synapse/storage/_base.py", line 119, in execute self._do_execute(self.txn.execute, sql, *args)
     File "/opt/venvs/matrix-synapse/lib/python3.5/site-packages/synapse/storage/_base.py", line 149, in _do_execute sql, *args
    psycopg2.IntegrityError: duplicate key value violates unique constraint "remote_media_cache_thumbnails_media_origin_media_id_thumbna_key"
    
  14. 13 remaining items

  15. richvdh commented on Sep 8, 2020

    @richvdh
    Member

    fixed by #7124.

  16. f0x52 commented on Oct 12, 2020

    @f0x52

    fixed by #7124.

    I'm still getting these errors in v1.21.0 which afaik includes that commit?

  17. richvdh commented on Oct 12, 2020

    @richvdh
    Member

    sigh. Please could you share the logs, with the stack trace?

  18. f0x52 commented on Oct 13, 2020

    @f0x52
    Oct 13 18:16:27 terra matrix-synapse[1297781]: DETAIL:  Key (media_origin, media_id, thumbnail_width, thumbnail_height, thumbnail_type, thumbnail_method)=(matrix.org, CENSORED, 14, 14, image/png, crop) already exists.
    Oct 13 18:16:27 terra matrix-synapse[1297781]: psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "remote_media_repository_thumbn_media_origin_id_width_height_met"
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     return func(sql, *args)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/storage/database.py", line 238, in _do_execute
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     self._do_execute(self.txn.execute, sql, *args)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/storage/database.py", line 212, in execute
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     txn.execute(sql, vals)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/storage/database.py", line 748, in simple_insert_txn
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     r = func(cursor, *args, **kwargs)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/storage/database.py", line 447, in new_transaction
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     return func(conn, *args, **kwargs)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/storage/database.py", line 641, in inner_func
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     result = func(conn, *args, **kw)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/twisted/enterprise/adbapi.py", line 297, in _runWithConnection
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     raise exception.with_traceback(traceback)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/twisted/python/compat.py", line 464, in reraise
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     compat.reraise(excValue, excTraceback)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/twisted/enterprise/adbapi.py", line 306, in _runWithConnection
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     return func(*args,**kw)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/twisted/python/context.py", line 85, in callWithContext
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     return self.currentContext().callWithContext(ctx, func, *args, **kw)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/twisted/python/context.py", line 122, in callWithContext
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     inContext.theWork = lambda: context.call(ctx, func, *args, **kw)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/twisted/python/threadpool.py", line 266, in <lambda>
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     result = inContext.theWork()
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/twisted/python/threadpool.py", line 250, in inContext
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     return await make_deferred_yieldable(
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/storage/database.py", line 646, in runWithConnection
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     result = await self.runWithConnection(
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/storage/database.py", line 569, in runInteraction
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     await self.runInteraction(desc, self.simple_insert_txn, table, values)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/storage/database.py", line 727, in simple_insert
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     await self.db_pool.simple_insert(
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/storage/databases/main/media_repository.py", line 362, in store_remote_media_thumbnail
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     await self.store.store_remote_media_thumbnail(
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/rest/media/v1/media_repository.py", line 589, in generate_remote_exact_thumbnail
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     file_path = await self.media_repo.generate_remote_exact_thumbnail(
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/rest/media/v1/thumbnail_resource.py", line 225, in _select_or_generate_remote_thumbnail
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     await self._select_or_generate_remote_thumbnail(
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/rest/media/v1/thumbnail_resource.py", line 66, in _async_render_GET
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     callback_return = await raw_callback_return
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/http/server.py", line 258, in _async_render
    Oct 13 18:16:27 terra matrix-synapse[1297781]:     callback_return = await self._async_render(request)
    Oct 13 18:16:27 terra matrix-synapse[1297781]:   File "/home/synapse/env/lib/python3.8/site-packages/synapse/http/server.py", line 230, in _async_render_wrapper
    Oct 13 18:16:27 terra matrix-synapse[1297781]: Traceback (most recent call last):
    Oct 13 18:16:27 terra matrix-synapse[1297781]: 2020-10-13 18:16:27,683 - synapse.http.server - 81 - ERROR - GET-474558 - Failed handle request via 'ThumbnailResource': <XForwardedForRequest at 0x7fc4bdb9aca0 method='GET' uri='/_matrix/m>
    Oct 13 18:16:25 terra matrix-synapse[1297781]: 2020-10-13 18:16:25,170 - synapse.rest.media.v1._base - 229 - WARNING - GET-474527 - Failed to write to consumer: <class 'Exception'> Consumer asked us to stop producing
    
  19. deepbluev7 commented on Oct 13, 2020

    @deepbluev7
    Contributor

    It seems like the migrations didn't delete the duplicate entries. The following two queries should fix that, if you run into those issues with the background update:

    DELETE FROM local_media_repository_thumbnails a USING (
          SELECT MIN(ctid) as ctid, media_id, thumbnail_width, thumbnail_height, thumbnail_type, thumbnail_method
            FROM local_media_repository_thumbnails
            GROUP BY media_id, thumbnail_width, thumbnail_height, thumbnail_type, thumbnail_method HAVING COUNT(*) > 1
          ) b
          WHERE a.media_id = b.media_id
          AND a.thumbnail_width = b.thumbnail_width
          AND a.thumbnail_height = b.thumbnail_height
          AND a.thumbnail_type = b.thumbnail_type
          AND a.thumbnail_method = b.thumbnail_method
          AND a.ctid <> b.ctid;
    
    DELETE FROM remote_media_cache_thumbnails a USING (
          SELECT MIN(ctid) as ctid, media_origin, media_id, thumbnail_width, thumbnail_height, thumbnail_type, thumbnail_method
            FROM remote_media_cache_thumbnails
            GROUP BY media_origin, media_id, thumbnail_width, thumbnail_height, thumbnail_type, thumbnail_method HAVING COUNT(*) > 1
          ) b
          WHERE a.media_id = b.media_id
          AND a.media_origin = b.media_origin
          AND a.thumbnail_width = b.thumbnail_width
          AND a.thumbnail_height = b.thumbnail_height
          AND a.thumbnail_type = b.thumbnail_type
          AND a.thumbnail_method = b.thumbnail_method
          AND a.ctid <> b.ctid;

    They delete the duplicates and should allow the migrations to pass, although I'm not sure, if that has any consequences.

  20. richvdh commented on Oct 13, 2020

    @richvdh
    Member

    @deepbluev7 I'm confused. I thought these exceptions were happening because the existing index (ADD CONSTRAINT local_media_repository_thumbn_media_id_thumbnail_width_thum_key UNIQUE (media_id, thumbnail_width, thumbnail_height, thumbnail_type)) was too strict. How can people have duplicates which prevent them running the migration?

    That doesn't seem to be @f0x52's problem, anyway, because he's reporting an error with the new constraint. @f0x52: is it possible that you are just seeing two requests for the same thumbnail at the same time, and they are racing? I think that would be #6755, which is a somewhat different problem :/

  21. deepbluev7 commented on Oct 13, 2020

    @deepbluev7
    Contributor

    @richvdh It seems their databases contains duplicates, which is why the migration fails. They may have deleted the index manually or something, not sure, but I did verify, that the db contains duplicates and deleting them fixes the issue. At least running the above query seemed to fix it for both people reporting the issue. But yeah, that seems to not apply to @f0x52's issue. No idea about that one.

  22. richvdh commented on Oct 13, 2020

    @richvdh
    Member

    If others have a problem with the migration, I'd like to know what their database schema looks like.

    For now I'm going to assume this is correctly fixed, and @f0x52 is seeing #6755.

  23. alturiak commented on Oct 13, 2020

    @alturiak

    @deepbluev7 @richvdh
    That is correct, I did delete the index as proposed as a workaround at #2182 (comment), which probably lead to the duplicates in the first place. They wouldn't probably appear in unmodified installations.

  24. richvdh commented on Oct 13, 2020

    @richvdh
    Member

    right, yes, people who have manually modified their database schema will now need to manually fix the resulting damage :).

  25. 532910 commented on Nov 19, 2020

    @532910

    It looks like this is fixed. Anyway I don't see this error more.

  26. Ezwen commented on Mar 12, 2021

    @Ezwen

    Hi there, I've just noticed I have errors in my synapse 1.29.0 logs, such as:

    synapse.storage.database - 957 - WARNING - GET-24- IntegrityError when upserting into remote_media_cache_thumbnails; retrying: duplicate key value violates unique constraint "remote_media_cache
    _thumbnails_media_origin_media_id_thumbna_key"
    

    Is it related to this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    z-bug(Deprecated Label)z-p2(Deprecated Label)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions