Skip to content

Removing/Invalidating the cache hangs the TypeOrm #3370

Closed

Description

@atomicus

Issue type:

[x] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I've stambled on a quite strange problem here.

I was using a built-in cache using SQL for some time without any problem.
Latelly I've added a cache invaliadation on one of my entry types by:

qCache = getConnection().queryResultCache;
            if (qCache !== undefined) {
                qCache.remove([cache_id]).then(() => {
                    logger.info(`Kill cache id ${cache_id} invalidated`)
                }).catch(err => {
                    logger.error(`Could not ivalidate cache ${err}`);
                })

Which, broke everything in a strange way.

On production, once this is called for the first time, the problem happens
On my test environment, sometimes few cache invalidations are required.

What is broke?

After a call to remove the cache, all following calls to repositories (finds, inserts) just "hang".
They do not return at all, they do not try to contact SQL (all connections are sleeping).
No new connections are made.

I've checked the implementation of remove cache method and it's obviously a simple delete on a table, which makes this even stranger.

I have no idea how to debug it more, I'll try to prepare some proof-of-concept repo for this, but right now I'm askign if you guys have any "ideas" which I could check.

Sidenotes:

  • My code creates up to 50-60 findMany on DB in short time
  • In between them, some might invalidate cache that was used by previous one or will be used very soon by next one
  • They are done one-by-one
  • Each of this operation might be called async and merge with previous (which was not a problem before adding cache invalidate) (I'm reacting to events send to the system)
  • Cache auto-invalidation by cache-life-time works as expected

Activity

  1. pleerock commented on Jan 6, 2019

    @pleerock
    Member

    Have no idea. Let's look on your reproduction example for me details. Best if you'll be able to create a PR with a failing test.

  2. Kononnable commented on Apr 20, 2019

    @Kononnable
    Contributor

    Closing as no response from the author.

  3. samie820 commented on Aug 14, 2020

    @samie820

    @Kononnable I ran into this same issue.
    Calling await getConnection().queryResultCache?.remove([user_related_entities_${user.id}]) the first time works fine, but any other future call to it (in a Controller) hangs the process and I'm forced to restart node

  4. philefstat commented on Aug 19, 2020

    @philefstat

    likewise - running into this issue, even in a simple script to sync configuration and remove the cached config.

  5. pscarey commented on Dec 11, 2020

    @pscarey

    Just adding another case of this. I think the issue should be re-opened.

    • We have a gRPC service which invalidates specific caches when certain RPCs are called.
    • After a certain amount of usage, the call to invalidate the cache would hang, and all other data-loading calls would hang.
    • We kill the service automatically if the healthcheck of SELECT 1 times out, but that's obviously a slow process.
  6. pscarey commented on Dec 11, 2020

    @pscarey

    Additional note, we're using the ioredis driver.

  7. pscarey commented on Dec 12, 2020

    @pscarey

    Frustratingly, I've been unable to reproduce the issue at this stage within the testing framework of TypeORM based off our issue.

    @philefstat @samie820 - would either of you be able to provide some additional info on what you were doing (possibly code snippets)?

  8. MieszkoGulinski commented on May 18, 2021

    @MieszkoGulinski

    I have a similar issue, using PostgreSQL and default cache (with a SQL table for cache results). After several updates to a table and clearing cache after that, queries start getting stuck.

    For the dataset size I had, it stopped getting stuck when I increased limit of simultaneous connections to PostgreSQL, using extra parameter in connection settings, but I suspect that it'll stop working if we had more data.

  9. gdrbyKo1 commented on Jan 21, 2022

    @gdrbyKo1

    This appears to be a bug in the implementation of DbQueryResultCache.remove(). If the optional queryRunner parameter is omitted, this method will allocate a new connection via DbQueryResultCache.getQueryRunner(), but will never release the connection afterwards.
    On the other hand, the DbQueryResultCache.storeInCache handles this case correctly and will call queryRunner.release() at the end if the caller didn't provide their own QueryRunner.

    To workaround the issue, you can call connection.createQueryRunner() on your own, pass the resulting query runner to the DbQueryResultCache.remove() call and then release it when .remove() resolves.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions