AWS lambda nodejs runtime: io: read/write on closed pipe

你说的曾经没有我的故事 提交于 2019-12-19 08:17:10

问题


I am trying to execute a couple of async requests from a lambda function. The first call resolveEndpoints() succeeds and the second fails with

2017/11/03 17:13:27 Function oauth.callbackHandler timed out after 3 seconds

2017/11/03 17:13:27 Error invoking nodejs6.10 runtime: io: read/write on closed pipe

The handler is:

exports.callbackHandler = async (event, context, callback) => {
    context.callbackWaitsForEmptyEventLoop = false;

    let endpoints: any = await resolveEnpoints();

    config.accessTokenUri = endpoints.token_endpoint;

    let tokenRequestPath =
    `http://localhost:7001${event.path}?code=${event.queryStringParameters.code}&realmId=${event.queryStringParameters.realmId}&`;

    let accessToken: any = await getAuthToken(tokenRequestPath);

    callback(undefined, {statusCode: 200, body: JSON.stringify(accessToken.data)});
};

If I remove the resolveEndpoint() call then getAuthToken() succeeds.

resolveEndpoint() returns a promise that resolves once the request has completed.

const resolveEnpoints = async () => {
    return new Promise((resolve, reject) => {
        request({
            url: config.sandboxEndpoint,
            headers: {
                'Accept': 'application/json'
            }
        }, (err, response) => {
            if (err) {
                reject(err);
            }

            let payload = JSON.parse(response.body);
            resolve(payload);
        });
    });
};

回答1:


Lambda's default timeout is 3 seconds and I was hitting that beyond a single HTTP call. Just need to update SAM Template to increase the timeout for handlers that needs to call multiple third party services.

Updated template with timeout set to 10 seconds allows the handler to run to completion.

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Description: |
  Data service

Resources:
  OAuthCallback:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: nodejs6.10
      CodeUri: ./build/services/quickbooks
      Handler: oauth2.callbackHandler
      Timeout: 10
      Events:
        AuthRoute:
          Type: Api
          Properties:
            Path: /oauth2/callback
            Method: get


来源:https://stackoverflow.com/questions/47089665/aws-lambda-nodejs-runtime-io-read-write-on-closed-pipe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!