Coder Social home page Coder Social logo

Comments (6)

esetnik avatar esetnik commented on July 23, 2024 1

@godu I think this should be reopened. serverless-offline is now recommending the use of --useWorkerThreads for node v11.7+ dherault/serverless-offline#508 (comment) because there's no way to debug when using --useSeparateProcesses, however --useWorkerThreads doesn't work with serverless-offline-sqs in that the process.env is still wrong for the function despite being launched in a separate worker thread.

from serverless-plugins.

pauldprice avatar pauldprice commented on July 23, 2024

The code is messing with process.env before calling the handler and then it changes it back after the handler is invoked. However, if you have any async code that is called by the handler then that code may run after process.env is set back. I'm running into this same issue and am wondering why process.env should be reset?

from serverless-plugins.

fusionbeam avatar fusionbeam commented on July 23, 2024

same here ...

from serverless-plugins.

abby-huisman avatar abby-huisman commented on July 23, 2024

I'm encountering an issue related to this as well. Is there a plan to change this? If so, is there an ETA?

from serverless-plugins.

godu avatar godu commented on July 23, 2024

Hi,

First of all, IMO, it's more a serverless-offline's bug than serverless-offline-{kinesis/sqs/dynamodb-stream}'s one.

Unfortunately, I think we need this kind of hack to avoid possible side-effects produce by some process.envs mutations.

I'll explain.

AWS Lambda allow us to defined different environment variables by lambda. However, serverless-offline, by default, execute all lambdas in the same thread, and share, in the facts, the same process.env value.

By example, imagine two lambdas lambda-red and lambda-blue :

functions:
  lambda-red:
    handler: handler.color
    environment:
      COLOR: red
  lambda-blue:
    handler: handler.color
    environment:
      COLOR: blue

with the same handler :

module.exports.color = (event, context, color) => {
  console.log(process.env.COLOR);
  setTimeout(() => {
    console.log(process.env.COLOR);
    cb();
  }, 1000);
};

If we trigger both in the same time, we got this output:

lambda-red: red
lambda-blue: blue
# 1s later
lambda-red: blue
lambda-blue: blue

A solution is to use --useSeparateProcesses options to isolate lambda's execution's context.

I think a good practice is to extract environment variables in the root scope, due to the process.env's immutability property that should have.

const {COLOR} = process.env;
module.exports.color = (event, context, color) => {
  console.log(COLOR);
  setTimeout(() => {
    console.log(COLOR);
    cb();
  }, 1000);
};

I'm open for suggestion. Maybe there is another solution.

from serverless-plugins.

thetumper avatar thetumper commented on July 23, 2024

Any chance to get a workaround or fix for those who cannot move to node v11.7+? I've been looking at other options, but have not found any simple alternative.

from serverless-plugins.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.