rfc2217/close(): fix race-condition: when stopping the read loop, do not set self._thread = None inside the thread itself as there may be a race-condition with the method close().
The method close() closes the socket, which stops the read loop. When the read loop stops, it set self._thread to None,
but if it set to None while close() is right between between the execution of "if self._thread:" and "self._thread.join()",
close() will raise an AttributeError.

Signed-off-by: Jerome Flesch <ext-jerome.flesch@flowbird.group>
diff --git a/serial/rfc2217.py b/serial/rfc2217.py
index d962c1e..53a2f43 100644
--- a/serial/rfc2217.py
+++ b/serial/rfc2217.py
@@ -613,7 +613,7 @@
         try:
             timeout = Timeout(self._timeout)
             while len(data) < size:
-                if self._thread is None:
+                if self._thread is None or not self._thread.is_alive():
                     raise SerialException('connection failed (reader thread died)')
                 buf = self._read_buffer.get(True, timeout.time_left())
                 if buf is None:
@@ -790,7 +790,6 @@
                         self._telnet_negotiate_option(telnet_command, byte)
                         mode = M_NORMAL
         finally:
-            self._thread = None
             if self.logger:
                 self.logger.debug("read thread terminated")