Repository navigation
Commit 78f6124
authored
Python: close native generators when stream invocations end (#14701)
### Motivation and Context
Closing a native function's streaming invocation after its first item
does not promptly close the underlying generator. With a retained
reference to that generator, its `finally` block remains suspended after
the invocation ends. The same occurs when `CancelledError` is injected
into the suspended invocation. A plugin that releases resources in
`finally` therefore keeps them live beyond the closed invocation.
This reproduces through both `KernelFunction.invoke_stream()` and
`Kernel.invoke_stream()` with ordinary decorated sync/async native
functions.
### Description
Use `closing`/`aclosing` around actual Python generators consumed by
`KernelFunction.invoke_stream`, and close the newly created
function-invocation stream owned by `Kernel.invoke_stream`. Ordinary
returned values and custom close-bearing iterable objects retain their
current behavior. Public signatures are unchanged.
### Validation
Base: `96c9e860db4d7c6da40652d9b421a31b05e98692`.
- Added 18 cases retaining the source generators so garbage collection
cannot make cleanup appear correct. They cover direct/Kernel entry,
sync/async sources, early `aclose`, injected cancellation, normal
exhaustion, original exception identity and an ordinary close-bearing
list that must remain untouched.
- Identical function/kernel suites against an asserted original-source
package root: **8 failed, 148 passed, 4 warnings**. Candidate: **156
passed, 4 warnings**. Both runs block socket connections; the
existing/dependency warnings are unchanged.
- Independent AI review reran those suites and **33 mocked prompt
tests**, plus 10 retained-source checks for real task cancellation,
awaited async finalizers and cleanup-error priority.
- Ruff lint/format checks on the three changed files and `git diff
--check`: passed.
Test environment: Python 3.13, local editable source and OpenAI 2.26.0
selected from the repository lockfile; not a complete locked
all-connectors environment. Full SDK/integration/provider suites were
not run.
Scope: the native-generator invocation path. Consumers still need to
close a retained stream when leaving their own iteration body, for
example with `aclosing`. The separate `invoke_prompt_stream` forwarding
layer and deeper provider wrappers are outside this fix; no general
provider-resource guarantee is claimed.
### Contribution Checklist
- [x] Added failing-first regression and compatibility coverage.
- [x] Followed contribution guidance and the existing Python source
style.
- [x] Ran focused local checks with external sockets blocked.
- [ ] Full SDK unit/integration matrix; the executed suites and
unchanged warnings are listed above.
AI disclosure: Codex implemented and tested this change, with an
independent AI review, as part of an autonomous upstream contribution
benchmark. No personal human code review is claimed.1 parent 703eb5f commit 78f6124
3 files changed
Lines changed: 100 additions & 16 deletions
File tree
- python
- semantic_kernel
- functions
- tests/unit/functions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
352 | 353 | | |
353 | 354 | | |
354 | 355 | | |
355 | | - | |
356 | | - | |
357 | | - | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
358 | 360 | | |
359 | | - | |
360 | | - | |
361 | | - | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
362 | 365 | | |
363 | 366 | | |
364 | 367 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
154 | 155 | | |
155 | 156 | | |
156 | 157 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
29 | 109 | | |
30 | 110 | | |
31 | 111 | | |
| |||
0 commit comments