Skip to content

Commit 553d79f

Browse files
committed
Lock known hosts file
Try and prevent concurrent read and writes to the known hosts file.
1 parent f6cd19a commit 553d79f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/clj_ssh/ssh.clj

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@
132132
(satisfies? protocols/Session x))
133133

134134
;;; Agent
135+
(def ^:private hosts-file
136+
"Something to lock to tray and prevent concurrent updates/reads to
137+
hosts file."
138+
(Object.))
139+
135140
(defn ssh-agent
136141
"Create a ssh-agent. By default a system ssh-agent is preferred."
137142
[{:keys [use-system-ssh-agent ^String known-hosts-path]
@@ -142,7 +147,8 @@
142147
(when use-system-ssh-agent
143148
(agent/connect agent))
144149
(when known-hosts-path
145-
(.setKnownHosts agent known-hosts-path))
150+
(locking hosts-file
151+
(.setKnownHosts agent known-hosts-path)))
146152
agent))
147153

148154
;;; Identities
@@ -391,9 +397,11 @@ keys. All other option key pairs will be passed as SSH config options."
391397
(defn connect
392398
"Connect a session."
393399
([session]
394-
(protocols/connect session))
400+
(locking hosts-file
401+
(protocols/connect session)))
395402
([session timeout]
396-
(protocols/connect session timeout)))
403+
(locking hosts-file
404+
(protocols/connect session timeout))))
397405

398406
(defn disconnect
399407
"Disconnect a session."

0 commit comments

Comments
 (0)