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

socket.setKeepAlive is not called, which could prevent timeouts during long requests #4439

Open
kuhe opened this issue Jun 6, 2023 · 1 comment
Labels
bug This issue is a bug. p2 This is a standard priority issue workaround-available

Comments

@kuhe
Copy link
Contributor

kuhe commented Jun 6, 2023

Describe the bug

When making very long duration requests, such as a lambda invoke that takes over 5 minutes to complete, the SDK may eventually timeout even if the response completes because the socket is not kept alive.

A current workaround seems to have fixed this issue for an internal customer, so we should add a socket.setKeepAlive call to the Node.js HTTP request.

var AWS = require('aws-sdk');

var handle = AWS.NodeHttpClient.prototype.handleRequest
if (!handle.override) {
  AWS.NodeHttpClient.prototype.handleRequest = function () {
    var ret = handle.apply(this, arguments);
    ret.on("socket", function (socket) {
      socket.setKeepAlive(true, 4 * 60 * 1000);
    });
    return ret;
  }
  AWS.NodeHttpClient.prototype.handleRequest.override = true;
}

Expected Behavior

long request >5 min completes

Current Behavior

long request >5 min reports a timeout even if the server completes the request successfully

Reproduction Steps

invoke a lambda transaction that takes a long time, 5-15 minutes.

Possible Solution

add socket.setKeepAlive(true, xxx) somewhere in the request creation process

Additional Information/Context

No response

SDK version used

current

Environment details (OS name and version, etc.)

EC2 to Lambda

@kuhe kuhe added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 6, 2023
@RanVaknin RanVaknin added p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Jun 6, 2023
@yenfryherrerafeliz
Copy link

A possible workaround would be:

import https from "https";

const keepAliveMsecs = 5 * 1000;
const tempHttpsRequestFn = https.request;
https.request = (options, callback) => {
    const clientRequest = tempHttpsRequestFn(options, callback);
    clientRequest.once('socket', (socket) => {
        console.log(keepAliveMsecs)
        socket.setKeepAlive(true, keepAliveMsecs || 0);
    });

    return clientRequest;
};

A quick example would be:

import AWS from "aws-sdk";
import https from "https";

const keepAliveMsecs = 5 * 1000;
const tempHttpsRequestFn = https.request;
https.request = (options, callback) => {
    const clientRequest = tempHttpsRequestFn(options, callback);
    clientRequest.once('socket', (socket) => {
        socket.setKeepAlive(true, keepAliveMsecs || 0);
    });

    return clientRequest;
};
const client = new AWS.Lambda({
    region: 'us-east-2'
});
const response = await client.invoke({
    FunctionName: "sleep",
    Payload: JSON.stringify({
        sleep: 100
    })
}).promise();
console.log(response)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. p2 This is a standard priority issue workaround-available
Projects
None yet
Development

No branches or pull requests

3 participants