Problems accessing a /tmp file created in a running function from a second function

Hi,

So here’s what I want to do (and it works fine testing locally with netlify dev, but not when on Netlify):

I have a function that writes a file to /tmp and then waits and checks whether it’s been modified by another function before returning. Now while that first function is still running and waiting, the second function can’t read that file, I get an error that no file was found. When the first function finishes after waiting up to 10 seconds (I believe that’s the limit?), the second function can then read that file.

As an example, I could do this in the first function:

let data = {hello: 'world'};
fs.writeFileSync('/tmp/data.json', JSON.stringify(data));
await waitTimer(); //a simple function that returns a Promise that resolves after some amount of time, like 8 seconds, just for testing
return {statusCode: 200, body: JSON.stringify({status: 'Done'})};

Then in the second function:

let dataFileJson = fs.readFileSync('/tmp/data.json');
let data = JSON.parse(dataFileJson);
return {statusCode: 200, body: JSON.stringify({status: 'Done', data})};

This will fail while the first function is still running, with an error that the file can’t be found, and as I mentioned before it’ll work fine once the first function is finished, when it returns.

So why can’t I access that file while the function is running? I’m writing it to /tmp when the function begins and yet it can’t be found until after the function ends. I can even read the file from the first function while it’s waiting, just not the second. Is there any way to make this work?

My aim is to send some data on one device, save it to a file and then read it from another device, a way of syncing a small amount of non-important data without having to use a database. So the first device sends the data and waits to either receive some merged data back, or for the other device to receive the data, or just timing out because nothing happened, then removing the data from the file.

I’m doing a similar thing on a NodeJS server that I created myself, but really wanted to get it working on serverless functions.

I’m aware that I could just return the first function after saving the data and start querying multiple times from the client side to check on progress, I just would have rather done that on the server. I’m really just curious why my method isn’t working.

Thanks for any help.

Also, just to add, it’s the same issue if I do everything within the same function.

While testing, I think I found that one function can’t access the /tmp file from another function, so when I say it worked fine after the first function ended, I think that was when I was testing in the same function, rather than having a second function, which I guess won’t work.

So still the same issue, but while using a single function for both examples of code.

Hey there, @Vyper248 :wave:

Thanks for reaching out and sharing this description. Can you please share your Netlify site as well as your function file? This will help us look into what is happening vs. what is expected.

Hi Hillary,

Thank you for getting back to me, but I decided to just switch to a different method of doing what I wanted considering the problem I was having, so aside from the code I posted above, there’s no function file that shows what’s happening anymore.

This is not possible. Functions are not meant to persist dat. Any data written there will anyways be cleared off within some time. Whatever you do within a function, you’re meant to send it as a response in the same invocation.

Yeah I realised that they’re not really designed for that, but I figured I could use a tmp file to transfer data. Doesn’t really matter if it’s cleared after some time, I only need it to be there for less than 30 seconds. Seems to be working ok with my new method.

Just thought it was a bit weird that I could create a file in the /tmp folder, but not access it from another instance of that function until the first instance had ended.

Only recently started learning about serverless functions, so still learning what they can and can’t do!