Skip to content

Commit b1aa869

Browse files
author
Stefan-Gabriel Muscalu
committed
Update: reworked storeFingerprint to await on appendFile
and lock the server ID while I/O event, thanks to @swist
1 parent 6e38ec1 commit b1aa869

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/v1/internal/ch-node.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ function loadFingerprint( serverId, knownHostsPath, cb ) {
5757
}
5858

5959
const _lockFingerprintFromAppending = {};
60-
function storeFingerprint( serverId, knownHostsPath, fingerprint ) {
60+
function storeFingerprint( serverId, knownHostsPath, fingerprint, cb ) {
6161
// we check if the serverId has been appended
6262
if(!!_lockFingerprintFromAppending[serverId]){
6363
// if it has, we ignore it
64-
return;
64+
return cb(null);
6565
}
6666

6767
// we make the line as appended
@@ -70,15 +70,11 @@ function storeFingerprint( serverId, knownHostsPath, fingerprint ) {
7070

7171
// we append to file
7272
fs.appendFile(knownHostsPath, serverId + " " + fingerprint + EOL, "utf8", (err) => {
73+
delete _lockFingerprintFromAppending[serverId];
7374
if (err) {
7475
console.log(err);
7576
}
76-
});
77-
78-
// since the error occurs in the span of one tick
79-
// after one tick we clean up to not interfere with anything else
80-
setImmediate(() => {
81-
delete _lockFingerprintFromAppending[serverId];
77+
return cb(err);
8278
});
8379
}
8480

@@ -145,8 +141,12 @@ const TrustStrategy = {
145141
if( knownFingerprint === serverFingerprint ) {
146142
onSuccess();
147143
} else if( knownFingerprint == null ) {
148-
storeFingerprint( serverId, knownHostsPath, serverFingerprint );
149-
onSuccess();
144+
storeFingerprint( serverId, knownHostsPath, serverFingerprint, (err) => {
145+
if (err) {
146+
return onFailure(err);
147+
}
148+
return onSuccess();
149+
});
150150
} else {
151151
onFailure(newError("Database encryption certificate has changed, and no longer " +
152152
"matches the certificate stored for " + serverId + " in `" + knownHostsPath +

0 commit comments

Comments
 (0)