microsoftgraph / microsoftgraph/msgraph-sdk-python

InMemoryBackingStore infinite loop

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

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

area:backing-store priority:p1 type:bug
Ngôn ngữ chính
Python
Star
630
Fork
96
Merge trung bình
15 giờ 20 phút
Pull request đã merge (30 ngày)
3

Mô tả

Describe the bug

The InMemoryBackingStore class is experiencing an infinite loop when setting values. This occurs because the set method subscribes a lambda function that calls set again, which in turn triggers the subscription callback, causing set to be called repeatedly.

        if isinstance(value, list):
            # if its a collection, subscribe to the collection's item BackingStores and use
            # the events to flag the collection property is "dirty"
            for item in value:
                if isinstance(item, BackedModel) and item.backing_store:
                    item.backing_store.is_initialization_completed = True
                    item.backing_store.subscribe(
                        lambda prop_key, old_val, new_val: self.set(key, value)
                    )

        self.__store[key] = value_to_add
        for sub in list(self.__subscriptions):
            self.__subscriptions[sub](key, old_value, value_to_add)

Because you're invoking the subscription which is calling set which is invoking the subscription.

Expected behavior

The set method should store the value and notify subscribers without causing an infinite loop.

How to reproduce
  1. Create an instance of InMemoryBackingStore.
  2. Set a value that is a BackedModel or a list containing BackedModel instances.
  3. The set method subscribes a lambda function that calls set again.
  4. The subscription callback is triggered, causing set to be called repeatedly.
SDK Version

No response

Latest version known to work for scenario above?

No response

Known Workarounds

Awful, but until an update, unsubscribing and re-subscribing around problematic code.

subscription_ids = []

# Collect all backing stores and sub ids as to not modify while iterating
for role in app.app_roles:
    if isinstance(role, BackedModel) and role.backing_store:
        for (
            sub_id,
            callback,
        ) in role.backing_store._InMemoryBackingStore__subscriptions.items():
            if callback.__name__ == "<lambda>":
                subscription_ids.append((role.backing_store, sub_id))

# Unsubscribe lambdas...
for backing_store, sub_id in subscription_ids:
    backing_store.unsubscribe(sub_id)

# Problematic code
for role in app.app_roles:
    if role.is_enabled != enable:
        role.is_enabled = enable

# Resubscribe lambdas...
for backing_store, sub_id in subscription_ids:
    backing_store.subscribe(
        lambda prop_key, old_val, new_val: role.backing_store.set(
            prop_key, new_val
        ),
        sub_id,
    )
Debug output
Click to expand log ```
</details>


### Configuration

_No response_

### Other information

_No response_

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

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.

Hướng nghiên cứu

Bắt đầu bằng cách tìm lớp InMemoryBackingStore và kiểm tra phương thức set của lớp này, đặc biệt là subscription được thêm vào cho các giá trị BackedModel và các danh sách. Tái hiện vấn đề với một BackedModel hoặc một danh sách các instance BackedModel và theo dõi các thông báo đến subscriber. Được xem là hoàn tất khi việc đặt giá trị lưu giá trị đó và thông báo cho các subscriber mà không gọi set một cách đệ quy hoặc đi vào vòng lặp vô hạn.

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

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
45/100

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.