Skip to content

MetadataService leaves sockets open #7538

@otaviomacedo

Description

@otaviomacedo

Checkboxes for prior research

Describe the bug

The MetadataService.fetchMetadataToken() leaves open sockets behind. The following TyepScript code shows the problem:

import { MetadataService } from "@aws-sdk/ec2-metadata-service";

async function main() {
  try {
    const metadataService = new MetadataService({
      httpOptions: {
        timeout: 1000,
      },
    });
    await metadataService.fetchMetadataToken();
  } catch (error) {
    // Not really interested in errors here
  }

  console.log('handles:', (process as any)._getActiveHandles());
}

main().catch(console.error);

The result of running this program is something like:

/Users/otaviom/.local/share/nvm/v22.16.0/bin/node --import file:/Applications/IntelliJ%20IDEA.app/Contents/plugins/nodeJS/js/ts-file-loader/node_modules/tsx/dist/loader.cjs /Users/otaviom/projects/cdk-app/bin/lab.ts
handles: [
  ...
  <ref *3> Socket {
    connecting: false,
    _hadError: false,
    _parent: null,
    _host: null,
    _closeAfterHandlingError: false,
    _events: ...,
    _readableState: ...,
    _writableState: ...,
    allowHalfOpen: false,
    _maxListeners: undefined,
    _eventsCount: 9,
    _sockname: null,
    _pendingData: 'PUT /latest/api/token HTTP/1.1\r\n' +
      'x-aws-ec2-metadata-token-ttl-seconds: 21600\r\n' +
      'Host: 169.254.169.254\r\n' +
      'Connection: keep-alive\r\n' +
      'Content-Length: 0\r\n' +
      '\r\n',
    _pendingEncoding: 'latin1',
    server: null,
    _server: null,
    parser: ...,
    _httpMessage: ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: true,
      _last: false,
      chunkedEncoding: false,
      shouldKeepAlive: true,
      maxRequestsOnConnectionReached: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: true,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      strictContentLength: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      _closed: false,
      _header: 'PUT /latest/api/token HTTP/1.1\r\n' +
        'x-aws-ec2-metadata-token-ttl-seconds: 21600\r\n' +
        'Host: 169.254.169.254\r\n' +
        'Connection: keep-alive\r\n' +
        'Content-Length: 0\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: nop],
      agent: [Agent],
      socketPath: undefined,
      method: 'PUT',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      joinDuplicateHeaders: undefined,
      path: '/latest/api/token',
      _ended: false,
      res: null,
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: [HTTPParser],
      maxHeadersCount: null,
      reusedSocket: false,
      host: '169.254.169.254',
      protocol: 'http:',
      ...
    },
    ...
  }
]

In some environments (e.g., GitHub workflows), this causes the Node process to hang.

Regression Issue

  • Select this option if this issue appears to be a regression.

SDK version number

@aws-sdk/ec2-metadata-service@3.940.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

Node version: v24.11.1

Reproduction Steps

Run the code above.

Observed Behavior

There are open sockets at the end of the program execution.

Expected Behavior

All sockets should have been closed.

Possible Solution

No response

Additional Information/Context

No response

Metadata

Metadata

Assignees

Labels

bugThis issue is a bug.needs-triageThis issue or PR still needs to be triaged.potential-regressionMarking this issue as a potential regression to be checked by team member

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions