microsoftgraph / microsoftgraph/msgraph-sdk-python

InMemoryBackingStore infinite loop

未关闭
#954 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

area:backing-store priority:p1 type:bug
主要语言
Python
星标
630
派生
96
平均合并
15 小时 20 分钟
30 天内合并 PR
3

描述

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_

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先定位 InMemoryBackingStore 类并检查其 set 方法,尤其是为 BackedModel 值和列表添加的订阅。使用一个 BackedModel 或 BackedModel 实例列表重现该问题,并跟踪 subscriber 通知。完成的标准是:设置值会存储该值并通知 subscriber,同时不会递归调用 set 或进入无限循环。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。