Unverified Commit 442342cb authored by liuzhe-lz's avatar liuzhe-lz Committed by GitHub
Browse files

Fix bug in IP detection (#3952)

parent d8127e02
......@@ -13,10 +13,10 @@ import * as fs from 'fs';
import * as net from 'net';
import * as os from 'os';
import * as path from 'path';
import * as timersPromises from 'timers/promises';
import * as lockfile from 'lockfile';
import { Deferred } from 'ts-deferred';
import { Container } from 'typescript-ioc';
import * as util from 'util';
import * as glob from 'glob';
import { Database, DataStore } from './datastore';
......@@ -49,39 +49,15 @@ function getExperimentsInfoPath(): string {
return path.join(os.homedir(), 'nni-experiments', '.experiment');
}
function mkDirP(dirPath: string): Promise<void> {
const deferred: Deferred<void> = new Deferred<void>();
fs.exists(dirPath, (exists: boolean) => {
if (exists) {
deferred.resolve();
} else {
const parent: string = path.dirname(dirPath);
mkDirP(parent).then(() => {
fs.mkdir(dirPath, (err: Error | null) => {
if (err) {
deferred.reject(err);
} else {
deferred.resolve();
}
});
}).catch((err: Error) => {
deferred.reject(err);
});
}
});
return deferred.promise;
async function mkDirP(dirPath: string): Promise<void> {
await fs.promises.mkdir(dirPath, { recursive: true });
}
function mkDirPSync(dirPath: string): void {
if (fs.existsSync(dirPath)) {
return;
}
mkDirPSync(path.dirname(dirPath));
fs.mkdirSync(dirPath);
fs.mkdirSync(dirPath, { recursive: true });
}
const delay: (ms: number) => Promise<void> = util.promisify(setTimeout);
const delay = timersPromises.setTimeout;
/**
* Convert index to character
......@@ -233,19 +209,21 @@ async function getIPV4Address(): Promise<string> {
const socket = dgram.createSocket('udp4');
socket.connect(1, '192.0.2.0');
for (let i = 0; i < 10; i++) { // wait the system to initialize "connection"
await yield_();
try { cachedIpv4Address = socket.address().address; } catch (error) { /* retry */ }
await timersPromises.setTimeout(1);
try {
cachedIpv4Address = socket.address().address;
socket.close();
return cachedIpv4Address;
} catch (error) {
/* retry */
}
}
cachedIpv4Address = socket.address().address; // if it still fails, throw the error
socket.close();
return cachedIpv4Address;
}
async function yield_(): Promise<void> {
/* trigger the scheduler, do nothing */
}
/**
* Get the status of canceled jobs according to the hint isEarlyStopped
*/
......
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as assert from 'assert';
import { getIPV4Address } from '../../common/utils';
it('getIpv4Address', async () => {
const ip1 = await getIPV4Address();
const ip2 = await getIPV4Address();
assert.match(ip1, /^\d+\.\d+\.\d+\.\d+$/);
assert.equal(ip1, ip2);
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment