pankajkoti commented on code in PR #40022:
URL: https://github.com/apache/airflow/pull/40022#discussion_r1631147608


##########
airflow/providers/sftp/hooks/sftp.py:
##########
@@ -558,13 +558,17 @@ async def get_mod_time(self, path: str) -> str:
 
         :param path: full path to the remote file
         """
-        ssh_conn = await self._get_conn()
-        sftp_client = await ssh_conn.start_sftp_client()
+        ssh_conn = None
         try:
+            ssh_conn = await self._get_conn()
+            sftp_client = await ssh_conn.start_sftp_client()
             ftp_mdtm = await sftp_client.stat(path)
             modified_time = ftp_mdtm.mtime
             mod_time = 
datetime.datetime.fromtimestamp(modified_time).strftime("%Y%m%d%H%M%S")  # 
type: ignore[arg-type]
             self.log.info("Found File %s last modified: %s", str(path), 
str(mod_time))
             return mod_time
         except asyncssh.SFTPNoSuchFile:
             raise AirflowException("No files matching")
+        finally:

Review Comment:
   Instead of using try finally to close the connection, can we use a context 
manager using the `with` clause similar to how we did here 
https://github.com/apache/airflow/pull/38881/files#diff-b93450b73d39b53428071b8dad04528c86fef7597937b4a302ae711b709e17d9R521
 in [PR](https://github.com/apache/airflow/pull/38881). The context manager 
should take care of closing the connection after exiting the with block 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to