Custom database script fails with socket hang up error

This could possibly happen if your scripts perform network-based asynchronous operations and during those operations there are additional logic that runs and throws unhandled errors within your script. The unhandled error could be causing an issue with the in-progress network operations.

With custom database script or rules you need to be careful to always call the callback function as expected and that there are also no unhandled errors. A quick way to troubleshoot your scripts/rules is to use console.log statements and review the generated logs.