#Unexpected File Persistence Across Appwrite Function Executions

2 messages · Page 1 of 1 (latest)

eager yew
#

I'm encountering an issue where files created during an Appwrite Function execution persist across subsequent executions. Specifically, when a function writes a file to the filesystem, that file remains available in the next execution, leading to errors.

Expected Behavior:

Each function execution should operate in an isolated environment with a clean filesystem, ensuring no residual data from previous runs.

Observed Behavior:

Files written during one execution are accessible in subsequent executions, indicating that the function's runtime environment is not being reset between runs.

Additional Context:

I am using Appwrite version 1.6.0 in a self-hosted setup. The function in question writes a file to a directory during execution. Upon re-execution, the function detects the existing file and throws an error.

Steps to Reproduce:
Deploy a function that writes a file to a specific directory.
Execute the function once; it should create the file successfully.
Execute the function again; it detects the existing file and throws an error.

Impact:
This behavior affects the reliability of functions that depend on a clean execution environment, potentially leading to unexpected errors and data inconsistencies.

Here is the function

import fs from 'fs';
import path from 'path';

async function testParallel() {
    const dirPath = path.join('./data/test-parallel');
    const testFilePath = path.join(dirPath, '/test.txt');

    if (!fs.existsSync(dirPath)) {
        fs.mkdirSync(dirPath, { recursive: true });
    }

    if (fs.existsSync(testFilePath)) {
        throw new Error('Test file already exists');
    }

    // write file
    fs.writeFileSync(testFilePath, 'This is a test file', 'utf8');
    console.log('Test file created at', testFilePath);

    // read file
    const data = fs.readFileSync(testFilePath, 'utf8');
    console.log('Test file data:', data);

    // make the script wait 90 seconds
    await new Promise((resolve) => {
        setTimeout(() => {
            console.log('Waited 90 seconds');
            resolve();
        }, 90000);
    });
}

export { testParallel };
rain musk
#

Specifically, when a function writes a file to the filesystem, that file remains available in the next execution, leading to errors.
This is expected because you only have one container on a self hosted instance unless you've tweaked things around to support multiples like using a load balancer or something else.

Note that a function container will stay active for 10 to 15 mins before its remove due to inactivity.

You can create a file, and use a unique name for it. Or better, use Databases <> Documents to store data thats new/unique per request.