TimeoutError: Request aborted due to timeout

Getting this error a lot when using the Fauna JS driver:

TimeoutError: Request aborted due to timeout

This timeout error is being returned almost instantly after attempting to create the transaction. My transaction is also actually still being written, but the Fauna JS driver returns this error anyway.

This is annoying, as I have logic that retries the transaction on a driver error. Quite regularly when creating a transaction, a timeout is returned once (sometimes twice) and then it is successful. Resultantly, I’ll end up with 2 or 3 documents instead of 1.

Code:

Fauna.createDecision = async (id, decision) => {
  decision = {
    id: id,
    ...decision,
    created: Now(),
  }

  let attempts = 5

  const createDecisionRetry = async (decision, attempts) => {
    const serverClient = new faunadb.Client({ secret: process.env.FAUNA_KEY_SECRET })

    try {
      await serverClient.query(
        Create(
          Collection('decisions'),
          { data: decision }
        )
      )
    } catch (e) {
      console.error('Error Fauna.createDecision', e)
      if (attempts > 1) {
        setTimeout(() => {
          createDecisionRetry(decision, attempts - 1)
        }, 200)
      }
    }
  }

  createDecisionRetry(decision, attempts)
}

I’m calling this function like so in an async function:

await Fauna.createDecision(id, decision)

Environment:

Execute: Lambda
Node version: 12
Fauna driver: 4.2.0

Not sure if this is an error with my code or with the driver?

There were timeout-related issues with the 4.2.0 driver. Can you try updating to 4.3.0?

Hi Ewan. I’ve just updated. Unfortunately timeouts are still very frequent.

I to am getting this error I am using faunadb": “^4.3.0”

I am first getting the currentIdentity ref, and then using streaming, right now I have the streaming turned off for debugging. I actually don’t need streaming for this particular feature, but wanted to test for features that will need it.

Here is my current code

const { user, loading, error } = useAuth();

  const [userData, setUserData] = useState({});

  useEffect(() => {

    const identify = authClient(user?.tokenId)
    .query(q.Get(q.CurrentIdentity()))
    .then(function (res) {
      return res;
    });

    async function setData() {
      try {
        const ref = await identify;

        setUserData({ ...ref.data, id: ref.ref.value.id });
      } catch (e) {
        console.log("e", e);
      }
    }
    setData();
  }, [loading]);

  async function report(e, id) {
    var data = "action" in e ? e["document"].data : e.data;
    setUserData({ ...data, id: id });
  }

  var stream;
  const startStream = async () => {
    const ref = await identify;

    stream = authClient(user?.tokenId)
      .stream.document(ref.ref)
      .on("snapshot", (snapshot) => {
        report(snapshot, ref.ref.value.id);
      })
      .on("version", (version) => {
        report(version, ref.ref.value.id);
      })
      .on("error", (error) => {
        console.log("Error:", error);
        stream.close();
        setTimeout(startStream, 1000);
      })
      .start();
  };

  //startStream();

In my network requests fauna says

current_identity: null

But it does have the correct Bearer code in authorization.

Not sure what is going on here.

Some help would be great.

Thanks ahead of time

One thing to note is, it does actually load after a few minutes. Also I am polling the user token cookie via a api route in next-js every 3 seconds because It seemed to fix this problem before, the token not being available fast enough for the fauna query to have it, but I don’t think that’s the issue, so I could turn polling of I think.

function useAuth() {
  const {
    data: user,
    error,
    mutate,
  } = useSWR("/api/user-profile", fetcher, { refreshInterval: 3 });

Ok so turning off the polling seemed to fix things. I think I over thought the problem for when I was debugging it initially