title: Why does accessing this variable fail after it is used in a thread?
body: <p>I am trying to use Google's Python API for Cloud Logging (here's their <a href="https://github.com/googleapis/python-logging" rel="nofollow noreferrer">library repo</a> and their <a href="https://googleapis.dev/python/logging/latest/index.html" rel="nofollow noreferrer">documentation</a>).</p>
  <p>I am also trying to use Python threads (specifically the <a href="https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool" rel="nofollow noreferrer">multiprocessing Pool</a> to speed up the processing of a list. The issue is that after using the <code>logger</code> in the thread pool, if I use it again in my <code>main</code> function it fails (or hangs indefinitely).</p>
  <h3>Example code</h3>
  <p>Here's some example code that fails for me:</p>
  <pre class="lang-python prettyprint-override"><code>from google.cloud import logging
  from multiprocessing import Pool
  from threading import Lock
  
  GENERAL_LOGGER = 'something'
  
  log_client = logging.Client()
  logger = log_client.logger(GENERAL_LOGGER)
  
  logger_lock = Lock()
  
  
  def main() -&gt; None:
      global logger
  
      logger.log_struct({
          &quot;msg&quot;: &quot;This works&quot;,
      })
  
      items = [1, 2, 3, 4, 5]
  
      with Pool(2) as p:
          p.map(test_thread, items)
  
      logger.log_struct({
          &quot;msg&quot;: &quot;This fails&quot;,
      })
  
  
  def test_thread(num) -&gt; None:
      global logger
  
      # do some intense task...
  
      # then log
      logger_lock.acquire()
      logger.log_struct({&quot;number&quot;: num}) # this succeeds
      logger_lock.release()
  
  
  if __name__ == '__main__':
      main()
  </code></pre>
  <p>For the above code, once it tries to log <code>This fails</code>, it does not log, and instead hangs indefinitely. I then have to <code>^C</code>, yielding the output below:</p>
  <pre><code>E0503 18:54:34.153646870  786847 ssl_transport_security.cc:510] Corruption detected.
  E0503 18:54:34.154107944  786847 ssl_transport_security.cc:486] error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
  E0503 18:54:34.154475815  786847 ssl_transport_security.cc:486] error:1000008b:SSL routines:OPENSSL_internal:DECRYPTION_FAILED_OR_BAD_RECORD_MAC
  E0503 18:54:34.154670516  786847 secure_endpoint.cc:208]     Decryption error: TSI_DATA_CORRUPTED
  E0503 18:54:34.154883721  786847 ssl_transport_security.cc:537] SSL_write failed with error SSL_ERROR_SSL.
  ^C
  Process ForkPoolWorker-2:
  Process ForkPoolWorker-1:
  Traceback (most recent call last):
    File &quot;/usr/lib/python3.8/multiprocessing/process.py&quot;, line 315, in _bootstrap
      self.run()
    File &quot;/usr/lib/python3.8/multiprocessing/process.py&quot;, line 108, in run
      self._target(*self._args, **self._kwargs)
    File &quot;/usr/lib/python3.8/multiprocessing/pool.py&quot;, line 125, in worker
      result = (True, func(*args, **kwds))
    File &quot;/usr/lib/python3.8/multiprocessing/pool.py&quot;, line 48, in mapstar
      return list(map(*args))
    File &quot;so.py&quot;, line 38, in test_thread
      logger_lock.acquire()
  KeyboardInterrupt
  Traceback (most recent call last):
    File &quot;so.py&quot;, line 44, in &lt;module&gt;
      main()
    File &quot;so.py&quot;, line 25, in main
      p.map(test_thread, items)
    File &quot;/usr/lib/python3.8/multiprocessing/pool.py&quot;, line 364, in map
      return self._map_async(func, iterable, mapstar, chunksize).get()
    File &quot;/usr/lib/python3.8/multiprocessing/pool.py&quot;, line 765, in get
      self.wait(timeout)
    File &quot;/usr/lib/python3.8/multiprocessing/pool.py&quot;, line 762, in wait
      self._event.wait(timeout)
    File &quot;/usr/lib/python3.8/threading.py&quot;, line 558, in wait
      signaled = self._cond.wait(timeout)
    File &quot;/usr/lib/python3.8/threading.py&quot;, line 302, in wait
      waiter.acquire()
  KeyboardInterrupt
  </code></pre>
  <h3>My real error</h3>
  <p>The actual code I am having an issue with is too long to put here, however it may be of help to put its printed error here (since it does seem to differ slightly):</p>
  <pre><code>E0503 18:36:42.763982000  769264 ssl_transport_security.cc:510] Corruption detected.
  E0503 18:36:42.764023851  769264 ssl_transport_security.cc:486] error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
  E0503 18:36:42.764032243  769264 ssl_transport_security.cc:486] error:1000008b:SSL routines:OPENSSL_internal:DECRYPTION_FAILED_OR_BAD_RECORD_MAC
  E0503 18:36:42.764038246  769264 secure_endpoint.cc:208]     Decryption error: TSI_DATA_CORRUPTED
  E0503 18:36:42.764096712  769264 ssl_transport_security.cc:537] SSL_write failed with error SSL_ERROR_SSL.
  Traceback (most recent call last):
    File &quot;/usr/local/lib/python3.8/dist-packages/google/api_core/grpc_helpers.py&quot;, line 73, in error_remapped_callable
      return callable_(*args, **kwargs)
    File &quot;/usr/local/lib/python3.8/dist-packages/grpc/_channel.py&quot;, line 946, in __call__
      return _end_unary_response_blocking(state, call, False, None)
    File &quot;/usr/local/lib/python3.8/dist-packages/grpc/_channel.py&quot;, line 849, in _end_unary_response_blocking
      raise _InactiveRpcError(state)
  grpc._channel._InactiveRpcError: &lt;_InactiveRpcError of RPC that terminated with:
          status = StatusCode.UNKNOWN
          details = &quot;Stream removed&quot;
          debug_error_string = &quot;{&quot;created&quot;:&quot;@1620067002.764071153&quot;,&quot;description&quot;:&quot;Error received from peer ipv4:172.217.11.202:443&quot;,&quot;file&quot;:&quot;src/core/lib/surface/call.cc&quot;,&quot;file_line&quot;:1067,&quot;grpc_message&quot;:&quot;Stream removed&quot;,&quot;grpc_status&quot;:2}&quot;
  &gt;
  
  Traceback (most recent call last):
    File &quot;/opt/scraper&quot;, line 331, in &lt;module&gt;
      main()
    File &quot;/opt/scraper&quot;, line 162, in main
      logger.log_struct({
    File &quot;/usr/local/lib/python3.8/dist-packages/google/cloud/logging_v2/logger.py&quot;, line 181, in log_struct
      self._do_log(client, StructEntry, info, **kw)
    File &quot;/usr/local/lib/python3.8/dist-packages/google/cloud/logging_v2/logger.py&quot;, line 134, in _do_log
      client.logging_api.write_entries([api_repr])
    File &quot;/usr/local/lib/python3.8/dist-packages/google/cloud/logging_v2/_gapic.py&quot;, line 149, in write_entries
      self._gapic_api.write_log_entries(request=request)
    File &quot;/usr/local/lib/python3.8/dist-packages/google/cloud/logging_v2/services/logging_service_v2/client.py&quot;, line 591, in write_log_entries
      response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
    File &quot;/usr/local/lib/python3.8/dist-packages/google/api_core/gapic_v1/method.py&quot;, line 145, in __call__
      return wrapped_func(*args, **kwargs)
    File &quot;/usr/local/lib/python3.8/dist-packages/google/api_core/retry.py&quot;, line 281, in retry_wrapped_func
      return retry_target(
    File &quot;/usr/local/lib/python3.8/dist-packages/google/api_core/retry.py&quot;, line 184, in retry_target
      return target()
    File &quot;/usr/local/lib/python3.8/dist-packages/google/api_core/grpc_helpers.py&quot;, line 75, in error_remapped_callable
      six.raise_from(exceptions.from_grpc_error(exc), exc)
    File &quot;&lt;string&gt;&quot;, line 3, in raise_from
  google.api_core.exceptions.Unknown: None Stream removed
  </code></pre>
  <p>It is potentially helpful to note that as long as I don't use threads everything works great, as expected. Something about using threads (specifically with Google's logger) causes this to break.</p>
  <p>I am not super familiar with Python threading, so maybe this is a novice mistake. Many thanks for any help in advance.</p>