question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Service async Initializer called multiple times (v3)

See original GitHub issue

While upgrading packages in one of my projects today, I noticed that starting from version 3.13 async initializer gets called multiple times (looks like once per injection?).

I’m wrapping knex as follows:

import { FastifyInstance } from 'fastify';
import { FastifyInstanceToken, Initializer, Inject, Service } from 'fastify-decorators';
import { Knex } from 'knex';

import { Database } from '@/database/Database';

@Service()
export class DatabaseService {
  readonly connection!: Knex;

  @Inject(FastifyInstanceToken)
  readonly instance!: FastifyInstance;

  public constructor() {
    this.connection = Database.getInstance().connection;
    this.instance.addHook('onClose', this.onClose.bind(this));
  }

  /**
   * This gets called on each @Inject call?
   * x1: Service A -> DatabaseService
   * x1: Service B -> Service A -> DatabaseService
   * etc
   */
  @Initializer()
  public async init() {
    await this.connection.raw(`select current_database()`);
  }

  private async onClose() {
    await this.connection.destroy();
  }
}

Nothing in documentation indicates that I should keep track on initialization myself (idempotent initializer), so I’m assuming its a bug?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:19 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
unematiiicommented, Nov 21, 2022

@L2jLiga Last version seems to fix all issues, I didn’t notice anything new. Thanks!

1reaction
unematiiicommented, Nov 15, 2022

So created a repro here.

npm run dev

and

$ curl localhost:3000

gives me

{"statusCode":500,"error":"Internal Server Error","message":"Cannot use 'in' operator to search for 'Symbol(fastify-decorators.service-injection)' in ServiceB"}%

While console:

[INFO] 21:10:25 ts-node-dev ver. 2.0.0 (using ts-node ver. 10.9.1, typescript ver. 4.8.4)
ServiceA::init()
ServiceA::init()
ServiceA::init()
ServiceA::init()
Hello from service ServiceA
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Calling an async method multiple times - Stack Overflow
The problem is that the application can call the Initialise method multiple times (user moving around randomly) passing in new set of ...
Read more >
How to do asynchronous operation in object constructor
... returning Promise Approach 1 - Start call in constructor, await completion in method Approach 2 - Use async initializer() method Approach 3...
Read more >
The Proper Way to Write Async Constructors in JavaScript
Produces verbose initialization at the call site. Requires the caller to be familiar with the lifecycle semantics and internals of the class.
Read more >
Why Is My Future/Async Called Multiple Times? - Flutter Igniter
Here we have a sample parent widget that rebuilds every 3 seconds. It's meant to represent any widget that triggers rebuilds like, for...
Read more >
Async/Await - Best Practices in Asynchronous Programming
Async methods returning void don't provide an easy way to notify the calling code that they've completed. It's easy to start several async...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found