Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: EBADF: bad file descriptor, write #72

Open
eyalzek opened this issue Oct 9, 2017 · 7 comments
Open

Error: EBADF: bad file descriptor, write #72

eyalzek opened this issue Oct 9, 2017 · 7 comments

Comments

@eyalzek
Copy link
Contributor

eyalzek commented Oct 9, 2017

Running version 0.3.9 of this plugin, my setup is as follows (all running in jenkins):
running using docker-compose I have 1 selenium hub instance, 2 chrome nodes, 1 instance of the frontend application.

once that is running, I'm constructing multiple commands similar to:

sh 'cd /usr/src/app && ' +
   "MOCHA_E2E_PARAMS='--grep=${tag} " +
   "--rootUrl=http://frontend:3000 --seleniumServer=hub " +
   "--browserName=chrome --screenshotDir=${env.WORKSPACE}/screenshots/selenium " +
   "-C -R mocha-jenkins-reporter -O junit_report_path=${env.WORKSPACE}/tests/test-results/selenium_chrome_${tag}.xml' " +
   "yarn run mocha-e2e"

I then run these inside a docker container using parallel. This has worked well using the xunit reporter (though I had other issues with it, unrelated to the runtime itself), but ever since integrating mocha-jenkins-reporter I'm getting the following error every time a suite fails:

fs.js:793
  return binding.writeString(fd, buffer, offset, length, position);
                 ^

Error: EBADF: bad file descriptor, write
    at Error (native)
    at Object.fs.writeSync (fs.js:793:18)
    at Runner.<anonymous> (/usr/src/app/node_modules/mocha-jenkins-reporter/lib/jenkins.js:308:10)
    at emitNone (events.js:91:20)
    at Runner.emit (events.js:185:7)
    at /usr/src/app/node_modules/mocha/lib/runner.js:812:12
    at done (/usr/src/app/node_modules/mocha/lib/runner.js:649:7)
    at Runner.next [as nextSuite] (/usr/src/app/node_modules/mocha/lib/runner.js:616:16)
    at Runner.uncaught (/usr/src/app/node_modules/mocha/lib/runner.js:737:17)
    at process.uncaught (/usr/src/app/node_modules/mocha/lib/runner.js:804:10)
    at emitOne (events.js:96:13)
    at process.emit (events.js:188:7)
    at process._fatalException (bootstrap_node.js:296:26)
error Command failed with exit code 7.

After looking at the source, this comes from the following line:
https://github.com/juhovh/mocha-jenkins-reporter/blob/master/lib/jenkins.js#L308

Since this is happening only after errors, I assume it's related to the onError handler I use, which basically dumps the client console and takes a screenshot:

    onError: function (error, testTitle, done) {
        error && console.error(error);
        const activeClient = require('./setup.js').browser.activeClient();
        dumpBrowsersConsole(activeClient)
            .then(() => saveScreenshot(activeClient, testTitle.replace('/', '_')))
            .execute(`return "LOG: Ending test ${testTitle}"`)
            .end()
            .call(done)
            .call(() => {
                process.exit(1);
            });
    }

and relevant functions:

function dumpBrowsersConsole (client, excludeLevels = []) {
    return client.log('browser').then(function (logmessage) {
        logmessage.value.forEach(entry => {
            if (excludeLevels.indexOf(entry.level) <= -1) {
                console.log('> ', entry.level, '> ', entry.message.substring(0, 500));
            }
        });
        return true;
    });
}
function saveScreenshot (client, testTitle) {
    if (!fs.existsSync(screenshotDir)) {
        fs.mkdirSync(screenshotDir);
    }
    const currentTestTitle = testTitle && testTitle.replace(/\s/g, '_');
    const screenshotPath = screenshotDir + '/e2e-screenshot-' + currentTestTitle + '.png';
    console.info('Creating screenshot', screenshotPath);
    return client
        .saveScreenshot(screenshotPath);
}

Again, this never happened with the xunit reporter, and this also doesn't happen when I try to replicate it locally. It seems like it's coming from the writing of the xml file itself, but AFAICT the xml file is written.

@eyalzek
Copy link
Contributor Author

eyalzek commented Oct 9, 2017

Mocha version is 3.4.2 and WebdriverIO version is 4.7.1.

I tried removing the process.exit but that's not it. I tried running a single suite without the parallel directive, but that fails with the same error. The XML reports are definitely generated, which makes this error very confusing..

@eyalzek
Copy link
Contributor Author

eyalzek commented Oct 9, 2017

So I forked the reporter and added some debugging:
eyalzek@1651497

and it looks like the end event handler is being called twice in the reporter, here is a gist of the output:
https://gist.github.com/eyalzek/0b64f1f751b30aa01bc66ad577dccdc0 (note that I removed the names from suites, but otherwise it's the same)

the second time it's called, the Non-test failures suite is added to the XML output, but since the file descriptor has already been closed after the previous write, this time it throws this error.
I am now trying to figure out if the bug is on our side or coming from the reporter. Even if this is coming from our code (which is likely), there must be a better way to handle this in the reporter code than failing in a non-descriptive way.

@eyalzek
Copy link
Contributor Author

eyalzek commented Oct 9, 2017

I disabled our afterEach hook so it's definitely not coming from our error handling. Also I noticed that the last error logged before the fs failure is:

{ TypeError: test.titlePath is not a function
    at Array.forEach (native)
    at Runner.<anonymous> (node_modules/mocha-jenkins-reporter/lib/jenkins.js:323:19) uncaught: true 

which is coming from mocha's base reporter: https://github.com/mochajs/mocha/blob/8b79c5d209249a5362538e35837afa3c2a581b28/lib/reporters/base.js#L227

I'm still not sure how this is related to the duplicate triggering of the end event...

@eyalzek
Copy link
Contributor Author

eyalzek commented Oct 10, 2017

Some more updates, this is what I think is happening:

A few things I don't understand:

  • why titlePath is undefined when running the epilogue
  • why the end event is being fired a second time after the failure

eyalzek pushed a commit to eyalzek/mocha-jenkins-reporter that referenced this issue Oct 10, 2017
since it is causing issues in some cases
juhovh#72
@Pourliver
Copy link

Any updates on this? I have the exact same problem, and I haven't been able to work around this.

jvah added a commit that referenced this issue Mar 28, 2018
@jvah
Copy link
Collaborator

jvah commented Mar 28, 2018

@Pourliver I tried to fix this in the latest 0.3.12 release, please try it out and let me know if it helped.

@Pourliver
Copy link

Well the fixe mutes the error, so I think we can see this as a fix :) Thanks for the fast reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants