Skip to content

Connection Denied when Running Multiple NPM Tasks in the Same Project #2426

Open
@jGleitz

Description

@jGleitz

We have multiple prettier-based tasks in our project, and we occasionally observe java.net.ConnectException: connection denied. This only happens in subprojects where multiple file types need to be formatted and use the same npm packages.

I’m not sure about the root cause, but I have a theory: NpmFormatterStepStateBase#npmRunServer has a race condition that would cause the behaviour we’re seeing.

The method works as follows:

  1. Check the server.port file in the installation directory, delete it if it exists.
  2. Start the Node.JS server.
  3. Wait for the server.port file to exist.
  4. Check the connection to the server using the port from server.port.

Crucially, two prettier tasks that use the same npm modules will share the installation directory. So we get the following race condition. I’ll explain using the example of a spotlessJson and a spotlessYaml task:

  1. spotlessJson and spotlessYaml start (approximately) simultaneously.
  2. spotlessJson and spotlessYaml pass the server.port file check before the other has started the server.
  3. spotlessJson and spotlessYaml start the NPM server.
  4. spotlessJson’s (for example) server starts first and creates server.port
  5. One of the two tasks sees the server.port, uses the server to format their files, and shuts down the server.
  6. The other task sees the server.port, tries to use the server, but can’t because the server has already shut down (it may even succeed in formatting some files).

@nedtwigg what do you think? Does this race condition exist or did I overlook something?

I’d consider contributing a fix if we agree that this is a likely root cause.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions