1# Copyright 2019 The gRPC Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Tests the gRPC Core shutdown path.""" 15 16import datetime 17import threading 18import time 19import unittest 20 21import grpc 22 23_TIMEOUT_FOR_SEGFAULT = datetime.timedelta(seconds=10) 24 25 26class GrpcShutdownTest(unittest.TestCase): 27 def test_channel_close_with_connectivity_watcher(self): 28 """Originated by https://github.com/grpc/grpc/issues/20299. 29 30 The grpc_shutdown happens synchronously, but there might be Core object 31 references left in Cython which might lead to ABORT or SIGSEGV. 32 """ 33 connection_failed = threading.Event() 34 35 def on_state_change(state): 36 if state in ( 37 grpc.ChannelConnectivity.TRANSIENT_FAILURE, 38 grpc.ChannelConnectivity.SHUTDOWN, 39 ): 40 connection_failed.set() 41 42 # Connects to an void address, and subscribes state changes 43 channel = grpc.insecure_channel("0.1.1.1:12345") 44 channel.subscribe(on_state_change, True) 45 46 deadline = datetime.datetime.now() + _TIMEOUT_FOR_SEGFAULT 47 48 while datetime.datetime.now() < deadline: 49 time.sleep(0.1) 50 if connection_failed.is_set(): 51 channel.close() 52 53 54if __name__ == "__main__": 55 unittest.main(verbosity=2) 56