Skip to content

Handle Node.js Debug Messages and Improve Port Termination #99

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
merged 4 commits into from
Mar 17, 2025

Conversation

Dacello
Copy link
Member

@Dacello Dacello commented Mar 16, 2025

Overview

This PR addresses two issues with Node.js communication:

  1. Node.js Debug Messages: Fixes "unexpected message in handle_info/2" errors when Node.js emits debugging information (like "Debugger listening on ws://..." messages).

  2. Terminal Reset Safety: Adds error handling around port operations during termination to prevent crashes when ports are in an invalid state.

Implementation Details

  • Added a proper handle_info/2 callback to handle messages from Node.js processes
  • Implemented a debug_mode configuration option that controls whether Node.js stdout/stderr messages are logged
  • Added safeguards to reset_terminal/1 to gracefully handle invalid ports during termination
  • Added comprehensive tests to validate both fixes

Usage

When working with Node.js applications that emit debug output:

# In config/dev.exs:
config :nodejs, debug_mode: true

Benefits

  • More stable operation in development environments using Node.js debugging tools
  • Prevents log flooding by making debug message logging configurable
  • Improves robustness during application shutdown
  • No breaking changes to existing APIs

Original Contribution

This PR builds on the work by @francois-codes, with refinements to follow best practices for configuration handling and error management.

defp reset_terminal(port) do
Port.command(port, "\x1b[0m\x1b[?7h\x1b[?25h\x1b[H\x1b[2J")
Port.command(port, "\x1b[!p\x1b[?47l")
try do
Copy link
Member Author

Choose a reason for hiding this comment

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

Added try/rescue to fix crashes when ports are closed during termination. Instead of blowing up with ArgumentError, we now just log and move on. This helps during dev reloading or when containers are shutting down - basically anytime processes are killed unpredictably.

@Dacello Dacello merged commit cf8bf5a into master Mar 17, 2025
2 checks passed
@Dacello Dacello deleted the chore/cleanup-debugger-output-handling branch March 17, 2025 15:11
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

Successfully merging this pull request may close these issues.

2 participants