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

Durable providers are unable to recover from errors being thrown #13953

Closed
1 of 15 tasks
dberardo-com opened this issue Sep 9, 2024 · 4 comments
Closed
1 of 15 tasks

Durable providers are unable to recover from errors being thrown #13953

dberardo-com opened this issue Sep 9, 2024 · 4 comments
Labels
needs triage This issue has not been looked into

Comments

@dberardo-com
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

the issue is described here and it is still relevant: #11664

Minimum reproduction code

check here: #11664

Steps to reproduce

No response

Expected behavior

durable provider should be decached and retried next time.

it would be nice if this is the default behavior even after the creation of the provider, i.e. in case of exceptions along its life

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

10.1.3

Packages versions

  "@nestjs/typeorm": "^10.0.0",

Node.js version

16

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@dberardo-com dberardo-com added the needs triage This issue has not been looked into label Sep 9, 2024
@micalevisk
Copy link
Member

here's a minimum reproduction of this: https://gitlab.com/micalevisk/nestjs-issue-11664

@dberardo-com
Copy link
Author

hope this gets some visibility soon, it is kind of a major bug indeed.

@micalevisk which versione of nestjs are you using? i havent tried the latest yet, dont know if the bug was perhaps fixed there (i am on 10.1 i guess)

@dberardo-com
Copy link
Author

dberardo-com commented Oct 23, 2024

btw, i have tested the patch in this PR: https://github.com/nestjs/nest/pull/11666/files and it seems to work, although it never made it to upstream ...

if you know how to use use patch-package (https://dev.to/zhnedyalkow/the-easiest-way-to-patch-your-npm-package-4ece), you only need this patch file and durable providers will be decached on errors:

@nestjs+core+10.3.3.patch

diff --git a/node_modules/@nestjs/core/injector/injector.js b/node_modules/@nestjs/core/injector/injector.js
index 53b1d5f..47f5741 100644
--- a/node_modules/@nestjs/core/injector/injector.js
+++ b/node_modules/@nestjs/core/injector/injector.js
@@ -70,6 +70,12 @@ class Injector {
             await this.resolveConstructorParams(wrapper, moduleRef, inject, callback, contextId, wrapper, inquirer);
         }
         catch (err) {
+            // https://github.com/nestjs/nest/pull/11666/files
+            console.error("[@nestjs/core/injector.js] - error on init of", contextId, err)
+            wrapper.removeInstanceByContextId(
+                this.getContextId(contextId, wrapper),
+                inquirerId,
+            );
             settlementSignal.error(err);
             throw err;
         }
diff --git a/node_modules/@nestjs/core/injector/instance-wrapper.js b/node_modules/@nestjs/core/injector/instance-wrapper.js
index 2f15b02..5bb87f1 100644
--- a/node_modules/@nestjs/core/injector/instance-wrapper.js
+++ b/node_modules/@nestjs/core/injector/instance-wrapper.js
@@ -75,6 +75,24 @@ class InstanceWrapper {
         }
         collection.set(contextId, value);
     }
+    // https://github.com/nestjs/nest/pull/11666/files
+    removeInstanceByContextId(contextId, inquirerId) {
+        console.error("[@nestjs/core/instance-wrapper.js] - removing context", contextId, inquirerId)
+        if (this.scope === common_1.Scope.TRANSIENT && inquirerId) {
+            return this.removeInstanceByInquirerId(contextId, inquirerId);
+        }
+
+        this.values.delete(contextId);
+    }
+
+    removeInstanceByInquirerId(contextId, inquirerId) {
+        let collection = this.transientMap.get(inquirerId);
+        if (!collection) {
+            return;
+        }
+
+        collection.delete(contextId);
+    }
     addCtorMetadata(index, wrapper) {
         if (!this[exports.INSTANCE_METADATA_SYMBOL].dependencies) {
             this[exports.INSTANCE_METADATA_SYMBOL].dependencies = [];

@kamilmysliwiec
Copy link
Member

Let's track this here #14133

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

3 participants