Skip to content

Conversation

Copy link

Copilot AI commented Nov 20, 2025

The pipe/socket communication with mpv's IPC could block indefinitely, had no retry logic for transient failures, and lacked proper error handling. This made progress reporting unreliable when mpv was slow to start or when temporary system issues occurred.

Changes

Retry with exponential backoff

  • 3 attempts with 100ms → 200ms → 400ms delays
  • Safe arithmetic using checked_pow to prevent overflow

Timeouts and bounds

  • 5-second read/write timeouts on Unix sockets
  • 4KB response size limit
  • Flush after write to ensure immediate transmission

Connection validation

  • Unix: Check socket file exists before connecting
  • Wait for IPC socket readiness after mpv startup
  • Validate JSON response structure and error fields

Error handling

  • Track consecutive failures (limit spam to first 5)
  • Context-aware error messages with attempt counts
  • Centralized IPC socket path configuration
  • Updated Windows Error API: Replaced deprecated Error::from_win32() with Error::from(std::io::Error) for forward compatibility with future windows crate versions
  • Descriptive error messages: Errors now include specific context (e.g., "Connection closed without data", "Response size exceeded limit") instead of generic Win32 error codes

Resource cleanup

  • RAII via std::fs::File's Drop (Windows)
  • Read loop terminates on complete JSON (newline delimiter)

Example

// Before: Could block forever
let mut stream = UnixStream::connect("/tmp/mpvsocket")?;
stream.read_to_string(&mut response)?;

// After: Timeouts, retries, bounds
let mut stream = UnixStream::connect(socket_path)?;
stream.set_read_timeout(Some(Duration::from_secs(5)))?;
// Read with 4KB max, retry on failure with exponential backoff

Windows Crate Compatibility Note: The code now uses Error APIs compatible with both windows 0.59 (current) and future versions. Windows 0.62.x has a known bug with windows-future 0.3.2 incompatibility; will upgrade to 0.63+ when available on crates.io.

Changes are backward compatible with no new dependencies.

Original prompt

检查一下代码,提出优化建议,尤其是使用pipe与mpv进行通信的部分,是否可以做的更加robust


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits November 20, 2025 23:01
Co-authored-by: Kosette <35268640+Kosette@users.noreply.github.com>
Co-authored-by: Kosette <35268640+Kosette@users.noreply.github.com>
Co-authored-by: Kosette <35268640+Kosette@users.noreply.github.com>
Co-authored-by: Kosette <35268640+Kosette@users.noreply.github.com>
Copilot AI changed the title [WIP] Review and suggest optimizations for pipe and mpv communication Make mpv IPC pipe communication robust with timeouts and retry logic Nov 20, 2025
Copilot AI requested a review from Kosette November 20, 2025 23:16
@Kosette
Copy link
Owner

Kosette commented Nov 22, 2025

@copilot 将windows crates升级到0.62.2版本,解决相关的兼容问题,比如Error::from_wind32()不存在

Copy link
Author

Copilot AI commented Nov 22, 2025

@copilot 将windows crates升级到0.62.2版本,解决相关的兼容问题,比如Error::from_wind32()不存在

Windows crate 0.62.x系列存在已知的严重bug:windows-future 0.3.2与windows-core 0.62.x不兼容,导致编译失败。这是上游issue,尚未在crates.io发布修复版本。

我已经更新代码替换了Error::from_win32()(该API在0.62.x中已变更),使用Error::from(std::io::Error)替代,这样代码在未来升级到0.63+时可以兼容。

目前建议:

  1. 保持windows = "0.59"版本(当前working版本)
  2. 代码已修改为兼容未来版本的Error API

当windows 0.63+发布到crates.io后,可以直接升级。

Co-authored-by: Kosette <35268640+Kosette@users.noreply.github.com>
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