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

Fix: Proxy should always return factory wrapped version of app #46

Closed
wants to merge 2 commits into from

Conversation

Pezmc
Copy link

@Pezmc Pezmc commented Jun 19, 2023

Before this change, when the server was first booted, the proxy, which is returned from restartable was pointing to a copy of app that's returned from fastify({ ...newOpts, serverFactory }), but from await factory(createApplication, opts, restartOptions) (with the user provided factory wrapper) after restarts.

On first boot app is a reference to what was returned from the fastify wrapper:

const app = fastify({ ...newOpts, serverFactory })

Object.setPrototypeOf(proxy, app)

On reboot newApp is a reference to the output of the users factory wrapper:

newApp = await factory(createApplication, opts, restartOptions)

Object.setPrototypeOf(proxy, newApp)

It was not possible to access the copy of app wrapped by factory, unless the server was restarted immediately after fist boot.

With this proposed change, the proxy always points to the latest copy of the app wrapped by the users factory.

Fixes #45

Checklist

@Pezmc Pezmc mentioned this pull request Jun 19, 2023
2 tasks
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening a PR! Can you please add a unit test?

@Pezmc Pezmc requested a review from mcollina June 23, 2023 15:39
@mcollina mcollina requested a review from ivan-tymoshenko June 23, 2023 18:26
@@ -743,3 +744,19 @@ test('should restart an app before listening', async (t) => {
await app.close()
t.ok(!app.server.listening)
})

test('should always proxy to the factory wrapped version of app', async (t) => {
async function userProvidedFactory () {
Copy link
Member

@ivan-tymoshenko ivan-tymoshenko Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async function userProvidedFactory () {
async function userProvidedFactory (fastify) {

You should pass a fastify factory param instance here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, if you pass the fastify factory as a param (see previous message), your test works on the current version. Not sure that I understand your point. Can you point me to where do you think you receive an app object instead of a proxy wrapper?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivan-tymoshenko Hi Ivan, actually, you're correct, while the proxy is set to const app = fastify({ ...newOpts, serverFactory }) in one place, and newApp = await factory(createApplication, opts, restartOptions) in another, because the proxy contains a reference to the latest copy of the object and the object is being mutated, they are the same.

Our codebase was missing the fastify factory as a param.

Copy link
Member

@ivan-tymoshenko ivan-tymoshenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments above.

@Pezmc Pezmc closed this Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to access underlying server
3 participants