[REQUEST] Detect and report infinite loops / recursive or self-referential data loading

Đang mở
#382 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
javascript, node.js
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu với luồng DataLoader.load và hành vi của hàm batch được thể hiện trong hai ví dụ. Tái hiện cả load tự tham chiếu và trường hợp phụ thuộc trong cùng một batch, sau đó xác định cách có thể báo cáo chúng mà không thay đổi hành vi tải thông thường. Hoàn thành khi các trạng thái treo này tạo ra cảnh báo hoặc lỗi hữu ích giúp xác định mối quan hệ load gây ra vấn đề.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

enhancement

If you accidentally create a self-referential DataLoader load, the loader silently creates a loop of promises which never resolve. This is obviously a programmer error, but it can be pretty hard to diagnose when things just silently don't work, if the recursive load isn't obvious across a complex codepath.

This code demonstrates the issue:

import DataLoader from 'dataloader';

const dumbDataLoader = new DataLoader(
  async nums => await Promise.all(nums.map(async n => await dumbDataLoader.load(n)))
);

console.log(await dumbDataLoader.load(3))

I'd expect to get some sort of warning or error from DataLoader to try to pin down the issue, but instead I only get this warning from Node (which in practise won't pop up for a more complex server):

Warning: Detected unsettled top-level await at index.js:7
console.log(await doubleDataLoader.load(3))

Worse, a variation of this can pop up even when the loads are technically not infinitely recursive, since you can have one batch waiting on a result from the same batch. Consider this code:

import DataLoader from 'dataloader';

const factDataLoader = new DataLoader(
  async nums => await Promise.all(nums.map(async n => {
    if (n == 0) return 1;
    return n * (await factDataLoader.load(n - 1));
  }))
);

// This works:
console.log(await factDataLoader.load(0));
console.log(await factDataLoader.load(1));

factDataLoader.clearAll();

// This does not:
console.log(await Promise.all([
  factDataLoader.load(0),
  factDataLoader.load(1),
]));

That isn't actually infinitely recursive, but because 0 and 1 end up in the same batch, and 1 depends on 0, and the result of 0 can't get to the computation around 1 because they're waiting on the same batch... this ends up hanging too. This is basically the same issue, but it's really hard to debug!

This example might look contrived, but this hit me in real-world code. My data loaders do data fetching and complex privacy checks. I have arbitrarily-nested posts, and you can only see a post if you can load the post's parent. If you try to load a post and its parent in the same batch of a data loader, it hangs without any error or warning!

Ngôn ngữ chính
JavaScript
Star
13.4k
Fork
522
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của graphql/dataloader

Tất cả issue của graphql/dataloader

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.