Azure-Samples / Azure-Samples/m365-inbox-serverless-agent-python

postdeploy hook fails with exit 3 on first deploy (race with function host startup for connector_extension system key)

オープン 初心者向け
#14 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Bicep
スター
11
フォーク
7
PR マージ指標
30日以内にマージされた PR はありません

説明

## Summary

On a fresh `azd up`, the `postdeploy` hook (`infra/scripts/configure-trigger.sh`) frequently fails with exit code 3 because it tries to read the Function App's `connector_extension` system key before the function host has finished starting and registering it.

## Repro

1. Fresh clone, fresh `azd env new`, fresh `azd up` into an empty resource group.
2. Provision + deploy succeed.
3. Postdeploy hook fires immediately and fails:

```
ERROR: step "cmdhook-postdeploy" failed: 'postdeploy' hook failed with exit code: '3',
Path: '/.../infra/scripts/configure-trigger.sh'.
```

The script emits (from lines 49–54):

> `ERROR: connector_extension system key not found on .`
> `The function host must start once with the connector-triggered agent before the key is created.`

## Root cause

Race between two async events:

1. ARM marks the Function App resource `Done` (what `azd` waits on).
2. The function host inside the app cold-starts, loads the connector-triggered binding, and registers the `connector_extension` system key.

The postdeploy hook runs as soon as (1) completes. On a cold first deploy, (2) hasn't happened yet, so `az functionapp keys list --query systemKeys.connector_extension` returns empty and the script exits 3.

Verified: ~45 seconds later the key was present, and `azd hooks run postdeploy` succeeded on the first retry with no other changes.

## Suggested fix

Add a bounded retry loop around the system-key fetch in `configure-trigger.sh` (and the `.ps1` sibling). Something like:

```bash
echo -e "${CYAN}Fetching connector_extension system key for ${functionAppName}...${NC}"
connectorKey=""
for i in 1 2 3 4 5 6 7 8 9 10; do
connectorKey=$(az functionapp keys list -g "$resourceGroup" -n "$functionAppName" \
--query "systemKeys.connector_extension" -o tsv 2>/dev/null || echo "")
[[ -n "$connectorKey" ]] && break
echo -e "${YELLOW} attempt $i/10: key not registered yet, waiting 15s for host start...${NC}"
sleep 15
done

if [[ -z "$connectorKey" ]]; then
echo -e "${RED}ERROR: connector_extension system key not found on ${functionAppName} after ~150s.${NC}" >&2
echo -e "${RED} Wait a minute and re-run: azd hooks run postdeploy${NC}" >&2
exit 3
fi
```

A 10×15s loop (~2.5 min) comfortably covers a Flex Consumption cold start while still failing fast if something is actually broken (e.g., missing binding, deploy didn't ship the connector-triggered function).

Optional belt-and-suspenders: nudge the host before polling — e.g., `az functionapp restart` followed by a short sleep — but the polling alone should be enough.

## Workaround today

```bash
# wait 30–60s after azd up reports done, then:
azd hooks run postdeploy
```

## Environment

- macOS, `azd` 1.25.6
- `azure-functions-core-tools` v4
- Fresh `rg-m365-test1`, default region

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

infra/scripts/configure-trigger.sh とその .ps1 対応ファイルから始め、報告された 49–54 行付近の system-key の取得に焦点を当てます。新しい azd up で再現し、その後、connector_extension キーが現れたときに制限付きポーリングによって postdeploy フックが成功すること、またキーが存在しないままの場合は再試行後も 3 で終了することを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
azure, bash
領域
cloud, devops
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
静か
明瞭さ
明確に書かれている
初心者へのやさしさ
72/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。