supabase / supabase

The open source Firebase alternative. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
https://supabase.com
Apache License 2.0
73.37k stars 7.08k forks source link

InvalidWorkerResponse: user worker failed to respond #20554

Closed nicetomytyuk closed 5 months ago

nicetomytyuk commented 9 months ago

Bug report

Describe the bug

When I try to make some calls to edge functions in a self hosted environment, usually it returns an error with status code 500 like this:

InvalidWorkerResponse: user worker failed to respond

The docker-compose file is update to the latest one in the repository.

To Reproduce

Create a self hosted environment using Docker, then try to create some functions in it and call it by using any of the official SDKs (I'm using JavaScript SDK in my case).

Here is a code snipper of one of the functions that usually returns the linked error:

import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
import { createClient } from "https://esm.sh/@supabase/supabase-js@2.39.3";

const corsHeaders = {
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Headers": "*",
};

const authHeader = "MY_SECRET_KEY";
const baseUrl = "https://rest.example.com/";

serve(async (req: Request) => {
    try {
      const request_data = await req.json();
      const supabase = createClient(baseUrl, authHeader);

      const { data, error } = await supabase.auth.admin.createUser({
          email: request_data.email,
          password: request_data.password,
          user_metadata: {
          username: request_data.username,
          company: request_data.company,
          created_by: request_data.created_by,
          role: request_data.role,
          },
          email_confirm: true,
      });
      if(error) throw error;

      return new Response(JSON.stringify(data), {
          headers: { ...corsHeaders, "Content-Type": "application/json" },
          status: 200,
      });
    } catch(err) {
        return new Response(JSON.stringify(err.message), {
          headers: { ...corsHeaders, "Content-Type": "application/json" },
          status: err.code,
        });
    }
});

Expected behavior

I'd expect the function to work without throwing the InvalidWorkerResponse error.

Screenshots

WhatsApp Image 2024-01-18 at 10 41 18_9dfd864d

System information

Additional context

When the function is run locally by using deno, it does not return the InvalidWorkerResponse and it's responses are even more faster than when the function is published to supabase.

For example, locally one of the functions was fetching data in less than 300ms, once publish to supabase, sometimes it returns InvalidWorkerResponse or the data if fetched in >=1s (first time calls).

encima commented 9 months ago

Thanks for reporting!

Locally running Supabase will almost always return faster responses as it is on the same machine; this would be expected behaviour. Adding network hops will increase latency.

Just to clarify: this error is happening in your local environment deployed by docker? or when the function is deployed to Supabase?

nicetomytyuk commented 9 months ago

Hi @encima,

The response is faster when I'm testing the functions locally by pointing the supabase instance to my remote self-hosted solution, so the functions are run with Deno locally.

When the same function is deployed to the remote server, it is slowed and often returns the InvalidWorkerResponse when the function is request.

So to clarify: this error is happening when the functions are deployed to self-hosted supabase on a remote server, when the functions are run by Deno locally there are no issues.

After further investigations, the issue seems to be related to edge-runtime:v1.29.1 and resolved in 1.31.0 by this PR request.

So I would suggest to change the edge-runtime version in docker-compose.yml to 1.31.0, I can open a PR for it.

encima commented 9 months ago

@nicetomytyuk thank you for the extra clarification!

That does indeed look related, can you confirm 1.31.0 resolves this and close if it does? If it does not, I will triage to the functions team. 🙏

nicetomytyuk commented 9 months ago

@encima I'd do some further tests before confirming that the 1.31.0 resolves the issue, I'd let you know as soon as possible.

encima commented 9 months ago

@nicetomytyuk No rush at all, you testing saves me testing so I am grateful for that!

nicetomytyuk commented 9 months ago

@encima I've did some more tests and after further investigation with @nyannyacha I've been able to resolve the issue by unlocking the CPU limit, so the PR to be highlited here is the PR-241 on edge-runtime.

I've created a PR that changes the edge-runtime version to v1.31.0 in docker-compose.yml and add some explanation on how to unlock the CPU limits in it.

So I can confirm that the edge-runtime:v1.31.0 does fix that issue.

For more details, you can follow the conversation about this issue and it's resolution with @nyannyacha on Discord.

Hallidayo commented 5 months ago

Hi everyone, I'm going to close this one but if you still have an issue with this please feel free to re open.