dherault / dherault/serverless-offline

Uncaught exception when trying to return an opensearch response

Open
#1,836 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5.3k
Forks
811
Avg merge
2d 4h
Merged PRs (30d)
3

Description

## Bug Report

I am currently testing using serverless offline to emulate an aws lambda that i will be deploying. It uses the default opensearch client to call an empty opensearch instance (with no data yet) running locally on my computer. When trying to return the search result I get the following error

```
× Uncaught exception
Environment: win32, node 22.11.0, framework 3.39.0 (local), plugin 7.2.3, SDK 4.5.1
Docs: docs.serverless.com
Support: forum.serverless.com
Bugs: github.com/serverless/serverless/issues

Error:
Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({})
at Worker.emit (node:events:507:17)
at Worker.emit (node:domain:489:12)
at [kOnErrorMessage] (node:internal/worker:326:10)
at [kOnMessage] (node:internal/worker:337:37)
at MessagePort. (node:internal/worker:232:57)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:816:20)
at MessagePort. (node:internal/per_context/messageport:23:28)
```

I can console.log the content of the variable just before trying to return it with no problems, the output is as follows:

```javascript
{
body: {
took: 6,
timed_out: false,
_shards: { total: 8, successful: 8, skipped: 0, failed: 0 },
hits: { total: [Object], max_score: 1, hits: [Array] }
},
statusCode: 200,
headers: {
'content-type': 'application/json; charset=UTF-8',
'content-length': '10917'
},
meta: {
context: null,
request: { params: [Object], options: {}, id: 1 },
name: 'opensearch-js',
connection: {
url: 'https://localhost:9200/',
id: 'https://localhost:9200/',
headers: {},
deadCount: 0,
resurrectTimeout: 0,
_openRequests: 0,
status: 'alive',
roles: [Object]
},
attempts: 0,
aborted: false
}
}
```

Returning any other value inside this object, or any other value whatsoever, outputs the returned value normally to the lambda invoke requester.

I thought maybe the output was too large but I tested returning the content of a file with millions of characters, much large then the opensearch search output and it worked perfectly. Maybe there is a nesting limit but I could not find it.

**Current Behavior**

When trying to return the object I get an error.

**Sample Code**

- file: serverless.yml

```yaml
service: my-service

plugins:
- serverless-offline

provider:
runtime: nodejs20.x
stage: dev

functions:
search:
handler: OpenSearchExample.search
```

- file: OpenSearchExample.js

```js
const { Client } = require("@opensearch-project/opensearch");

const host = "localhost";
const protocol = "https";
const auth = "admin:[OPENSEARCH_INITIAL_ADMIN_PASSWORD]"
const port = 9200;

const client = new Client({
node: protocol + "://" + auth + "@" + host + ":" + port,
ssl: {
rejectUnauthorized: false
}
});

exports.search = async () => {
response = await client.search()
console.log(response)
return {
body: response,
statusCode: 200
}
}
```

- file: compose.yml
```yml
services:
opensearch-node1:
image: opensearchproject/opensearch:2
container_name: opensearch-node1
environment:
- cluster.name=opensearch-cluster
- node.name=opensearch-node1
- discovery.type=single-node
- discovery.seed_hosts=opensearch-node1
- bootstrap.memory_lock=true # along with the memlock settings below, disables swapping
- OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m # minimum and maximum Java heap size, recommend setting both to 50% of system RAM
- OPENSEARCH_INITIAL_ADMIN_PASSWORD=${OPENSEARCH_INITIAL_ADMIN_PASSWORD} # Sets the demo admin user password when using demo configuration, required for OpenSearch 2.12 and higher
ulimits:
memlock:
soft: -1
hard: -1
nofile:
soft: 65536 # maximum number of open files for the OpenSearch user, set to at least 65536 on modern systems
hard: 65536
volumes:
- opensearch-data1:/usr/share/opensearch/data
ports:
- 9200:9200
- 9600:9600 # required for Performance Analyzer
networks:
- opensearch-net
opensearch-dashboards:
image: opensearchproject/opensearch-dashboards:2
container_name: opensearch-dashboards
ports:
- 5601:5601
expose:
- '5601'
environment:
OPENSEARCH_HOSTS: '["https://opensearch-node1:9200"]'
networks:
- opensearch-net

volumes:
opensearch-data1:

networks:
opensearch-net:
```

**Expected behavior/code**

To see on the invoker output the value that was printed to console.

**Environment**

- `serverless` version: 3.39.0
- `serverless-offline` version: 13.6.0
- `node.js` version: 22.11.0
- `OS`: windows 11 win32

Contributor guide

Open the contributing guide

Research direction

Reproduce the return path using the serverless.yml and OpenSearchExample.js samples with serverless-offline and the compose.yml OpenSearch service. Start by tracing how serverless-offline handles returned values and the Worker error shown in the stack trace. Done means an OpenSearch search response can be returned to the Lambda invoke requester without an uncaught exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, javascript, node.js
Domain
backend, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.