Skip to content

Throw error on null bitmap to prevent crash #54

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

Merged

Conversation

lewisl9029
Copy link
Contributor

@lewisl9029 lewisl9029 commented Mar 7, 2021

I've been exploring using nut.js (and more recently the libnut library directly) to control a headless Windows machine monitored through RDP (Using the official Remote Desktop app).

The setup generally worked fine, but I noticed the node process ends up consistently crashing as soon as I disconnect the RDP session.

Dug into it a bit this weekend and found that the crash was due to this null returning branch in copyMMBitmapFromDisplayInRect being unhandled downstream.

This PR handles the null case by throwing an exception so it can be handled in userland appropriately (in my case a simple retry after some delay) instead of crashing the entire program.

It's been a while since I wrote any C, so please let me know if there's any other cleanup steps necessary before throwing the exception that I might have missed. Thanks!

@s1hofmann s1hofmann self-requested a review March 8, 2021 06:56
@s1hofmann s1hofmann self-assigned this Mar 8, 2021
Copy link
Member

@s1hofmann s1hofmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lewisl9029 👋

Thanks for your contribution!
You're right, this should definitely be handled in userland.

Just two remarks:

  1. Would you mind creating a corresponding issue for this PR? I'd like to link to issues instead of merged PRs for the changelog
  2. Is there a way to properly test this change on all platforms? I'd would love having a test for every PR, so maybe you want to take another look at it (maybe even join our dev channels on Discord?). If you don't find time for it, no problem. I'll see for myself then.

@lewisl9029
Copy link
Contributor Author

Hi there!

Here's the issue I've created, also listed out the repro steps: #55

Re: testing, I'd love to be able to write a test for this, but we'd either have to repro the conditions that led to the null branch in copyMMBitmapFromDisplayInRect in a CI environment somehow, which I have no idea how to do since it involves connecting/disconnecting from RDP, or mock the function in C somehow, when the existing tests seem to be focused on testing the public interface in JS and doesn't appear to have that ability.

Would love to hear if you have some suggestions on how we could approach testing this though.

@s1hofmann
Copy link
Member

One approach to test this on Linux would be to run a test without Xvfb, but I don’t have approaches for Windows and macOS. Making the cross-platform screen grabbing testable would require a little restructuring, which I think doesn’t have to happen here.

Let‘s deal with improving testability of screen grabbing in a separate issue.

@s1hofmann s1hofmann merged commit 938ac42 into nut-tree:develop Mar 11, 2021
@lewisl9029 lewisl9029 deleted the handle-null-screenshot-bitmap branch March 28, 2021 20:13
@lewisl9029 lewisl9029 restored the handle-null-screenshot-bitmap branch October 18, 2021 04:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

screen.capture crashes node process on Windows under certain conditions
2 participants