Skip to content

Commit 567ea5f

Browse files
committed
Fix bugs
1 parent 47cf5bf commit 567ea5f

File tree

7 files changed

+61
-13
lines changed

7 files changed

+61
-13
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ The method `easy_invoke` will attempt to infer the return value in the given Clo
106106

107107
**Tips:** If an error occurs for a given Cloud Function runtime, an `InvokeError` exception is thrown. The above exceptions are defined in the `tencent.cloud.serverless.functions.errors` module.
108108

109+
#### Close product client
110+
When the instantiated client is no longer in use, we should explicitly close it:
111+
```python
112+
function_client.close()
113+
```
114+
109115
See the [examples source code](examples) for the complete demo code.
110116

111117
### Other Tencent Cloud Products

examples/quickstart.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ def main():
6262
print('error: ' + str(error), file = sys.stderr)
6363
except errors.ActionError as error:
6464
print('error: ' + str(error), file = sys.stderr)
65+
66+
# When the instantiated client is no longer in use, we should
67+
# explicitly close it:
68+
69+
function_client.close()
6570

6671
if __name__ == '__main__':
6772
main()

packages/tencent-cloud-sdk-core/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def read_readme_content() -> str:
3131

3232
setuptools.setup(
3333
name = 'tencent-cloud-sdk-core',
34-
version = '0.2.5',
34+
version = '0.2.6',
3535
packages = [
3636
'tencent.cloud.core'
3737
],

packages/tencent-cloud-sdk-core/tencent/cloud/core/client.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,57 @@ def __init__(self,
188188

189189
self.__initialized = True
190190

191+
async def close_async(self):
192+
'''
193+
Close the client.
194+
'''
195+
196+
await self.__request_client.close()
197+
198+
def close(self):
199+
'''
200+
Close the client.
201+
'''
202+
203+
self._get_event_loop().run_until_complete(self.close_async())
204+
191205
def __del__(self):
192-
if not self.__initialized: # Base client instance construction error
206+
if not self.__initialized or not hasattr(self, '_BaseClient__request_client'):
207+
# Base client instance construction error
193208
return
194209

195-
# The event loop instance running in the hyperthreaded context
196-
# of the underlying client should not be closed.
210+
if self.__request_client.closed:
211+
# Internal request client has been closed
212+
return
197213

198214
if self._get_event_loop().is_closed():
215+
# The event loop instance running in the hyperthreaded context
216+
# of the underlying client should not be closed.
199217
raise RuntimeError('destroy the client before the event loop closes')
200218

201219
if not self._get_event_loop().is_running():
202220
self._get_event_loop().run_until_complete(self.__request_client.close())
203221
else:
204-
self._get_event_loop().create_task(self.__request_client.close())
222+
# This happens when the caller does not explicitly close the client.
223+
#
224+
# When the GC reclaims the client object, we cannot immediately close
225+
# the internal session if there are other coroutines running. At the
226+
# same time, GC also recycles and frees internal session object.
227+
# When the internal session object is released, if it is not explicitly
228+
# closed, it raises a warning and causes an internal connector leak.
229+
#
230+
# To avoid raising warning and connector leak, we need to trick the
231+
# internal session into thinking it is closed. We will schedule the
232+
# real close handler to run after the other coroutines have exited.
233+
#
234+
# This is an invasive solution that is not elegant.
235+
236+
async def _internal_session_close():
237+
setattr(self.__request_client.connector, '_closed', False)
238+
await self.__request_client.close()
239+
240+
setattr(self.__request_client.connector, '_closed', True)
241+
self._get_event_loop().create_task(_internal_session_close())
205242

206243
@property
207244
def error_manager(self) -> errors.ErrorManager:

packages/tencent-cloud-sdk-serverless-database/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def read_readme_content() -> str:
3131

3232
setuptools.setup(
3333
name = 'tencent-cloud-sdk-serverless-database',
34-
version = '0.1.3',
34+
version = '0.1.4',
3535
packages = [
3636
'tencent.cloud.serverless.database'
3737
],
@@ -58,6 +58,6 @@ def read_readme_content() -> str:
5858
# 'Development Status :: 4 - Beta'
5959
],
6060
install_requires = [
61-
'tencent-cloud-sdk-core>=0.2.5'
61+
'tencent-cloud-sdk-core>=0.2.6'
6262
]
6363
)

packages/tencent-cloud-sdk-serverless-functions/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def read_readme_content() -> str:
3131

3232
setuptools.setup(
3333
name = 'tencent-cloud-sdk-serverless-functions',
34-
version = '0.2.4',
34+
version = '0.2.5',
3535
packages = [
3636
'tencent.cloud.serverless.functions'
3737
],
@@ -58,6 +58,6 @@ def read_readme_content() -> str:
5858
# 'Development Status :: 4 - Beta'
5959
],
6060
install_requires = [
61-
'tencent-cloud-sdk-core>=0.2.5'
61+
'tencent-cloud-sdk-core>=0.2.6'
6262
]
6363
)

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def read_readme_content() -> str:
3131

3232
setuptools.setup(
3333
name = 'tencent-cloud-sdk',
34-
version = '0.2.6',
34+
version = '0.2.7',
3535
keywords = 'tencent-cloud sdk-python',
3636
license = 'MIT License',
3737
author = 'MIEK',
@@ -54,8 +54,8 @@ def read_readme_content() -> str:
5454
],
5555
install_requires = [
5656
'tencent-cloud-sdk-auth>=0.2.2',
57-
'tencent-cloud-sdk-core>=0.2.5',
58-
'tencent-cloud-sdk-serverless-functions>=0.2.4',
59-
'tencent-cloud-sdk-serverless-database>=0.1.3'
57+
'tencent-cloud-sdk-core>=0.2.6',
58+
'tencent-cloud-sdk-serverless-functions>=0.2.5',
59+
'tencent-cloud-sdk-serverless-database>=0.1.4'
6060
]
6161
)

0 commit comments

Comments
 (0)