【问题标题】:aws lambda c++ runtime seg fault (but works locally)aws lambda c++ 运行时段错误(但在本地工作)
【发布时间】:2020-11-21 12:19:46
【问题描述】:

我正在尝试在 aws c++ 运行时上运行一个简单的示例,但是当我调用 lambda 函数时,它会不断出现段错误。首先,以下示例中显示的示例:https://aws.amazon.com/blogs/compute/introducing-the-c-lambda-runtime/ 工作正常。所以,现在,我从一个桶中取一些东西,如下所示:

#include <aws/core/Aws.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <awsdoc/s3/s3_examples.h>
#include <vector>
#include <rapidcsv.h>
#include <torch/torch.h>
#include <torch/script.h> // One-stop header.
#include <iostream>
#include <math.h>
#include <memory>
#include <aws/lambda-runtime/runtime.h>

using namespace torch::indexing;
using namespace Aws::Utils;
using namespace aws::lambda_runtime;

char const TAG[] = "LAMBDA_ALLOC";

static invocation_response my_handler(invocation_request const &req)
{
   if (req.payload.length() > 42)
   {
      return invocation_response::failure("error message here" /*error_message*/,
                                          "error type here" /*error_type*/);
   }

   Aws::SDKOptions options;
   Aws::InitAPI(options);

   const Aws::String bucket_name("buckname");
   const Aws::String object_name("fname");

   Aws::S3::S3Client s3_client;
   Aws::S3::Model::GetObjectRequest object_request;
   object_request.SetBucket(bucket_name);
   object_request.SetKey(object_name);
   Aws::S3::Model::GetObjectOutcome get_object_outcome =

   s3_client.GetObject(object_request);
   auto &retrieved_file = get_object_outcome.GetResultWithOwnership().GetBody();
   rapidcsv::Document doc(retrieved_file, rapidcsv::LabelParams(-1, -1)); /// this is where the problem is.

   const Aws::String objectKey2("model.tar");
   Aws::S3::S3Client s3_client2;
   Aws::S3::Model::GetObjectRequest object_request2;
   object_request2.SetBucket(bucket_name);
   object_request2.SetKey(objectKey2);
   Aws::S3::Model::GetObjectOutcome get_object_outcome2 =
   s3_client2.GetObject(object_request2);
   auto &retrieved_file2 = get_object_outcome2.GetResultWithOwnership().GetBody();

   torch::jit::script::Module module;
   module = torch::jit::load(retrieved_file2);  /// this is also where the problem is.
   std::cout << "Model Load ok\n";

   Aws::ShutdownAPI(options);

   return invocation_response::success(tensor_string /*payload*/,
                                       "application/json" /*MIME type*/);
}

int main()
{

   run_handler(my_handler);
   return 0;
}

问题是,当我在本地(debian、cmake)运行它时,它可以按预期编译和工作,但是当我为 aws 运行时编译它时,我得到了 seg 错误。

我已将问题缩小到两行:

rapidcsv::Document doc(retrieved_file, rapidcsv::LabelParams(-1, -1)); /// this is where the problem is.

还有,

module = torch::jit::load(retrieved_file2);  /// this is also where the problem is.

我从这里只使用 rapidcsv 标头库:https://github.com/d99kris/rapidcsv,正如我所说,它在本地运行良好。

我怀疑运行时不知何故无法找到 rapidcsv(但这仍然无法解释为什么它在 module = torch::jit::load(retrieved_file2); 上出现段错误:(

任何指针都会很棒!

【问题讨论】:

  • get_object_outcome.GetResultWithOwnership() 不返回 RAII 对象(大部分立即销毁),使auto &amp;retrieved_file 悬空引用?
  • 示例似乎改用GetResult()
  • @JaI 将立即尝试这个.. 但它如何在本地工作?
  • @Jarod42:你能告诉我getResult() 的用处吗?
  • @Jarod42 是的,我在文档中看到它:github.com/awslabs/aws-lambda-cpp/blob/master/examples/s3/…

标签: c++ amazon-web-services aws-lambda


【解决方案1】:

现在提供输入可能有点晚了,但无论如何,如果有人在 AWS Lambda 上遇到类似的 rapidcsv 问题:

本地和 AWS 之间的一个潜在区别是可用 RAM。通过遵循basic tutorial,最终会得到一个具有 128 MB RAM (aws lambda create-function ... --memory-size 128) 的 lambda 函数。根据要处理的数据大小,这可能偏低。

在 AWS Lambda 控制台中,可以测试运行 lambda 函数并查看已配置和使用的最大内存,例如:

Resources configured   128 MB
Max memory used        44 MB

检查最大使用量是否低于配置水平可能值得。

【讨论】:

    猜你喜欢
    • 2019-12-15
    • 2019-05-07
    • 2021-04-02
    • 1970-01-01
    • 2019-04-20
    • 2017-06-05
    • 2023-03-12
    • 1970-01-01
    • 2022-11-25
    相关资源
    最近更新 更多