anthropics / anthropics/skills

xlsx recalc.py crashes on Windows: unguarded socket.AF_UNIX access

Aperta
#1,120 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Python
Stelle
176k
Fork
20.8k
Merge medio
7h 21m
PR unite (30g)
5

Descrizione

## Summary

`skills/xlsx/scripts/office/soffice.py` — the `_needs_shim()` function tries to construct an `AF_UNIX` socket to detect whether the LD_PRELOAD shim is needed for sandboxed Linux environments. On Windows Python builds where `socket.AF_UNIX` is not exposed as an attribute, the attribute lookup raises `AttributeError` (not `OSError`), which the surrounding `try/except OSError` does not catch. As a result, `recalc.py` is unusable on these builds — it crashes before any LibreOffice work begins.

## Environment

- Windows 11 Home (Build 26200)
- Python 3.14.4 from a uv-managed install (`cpython-3.14-windows-x86_64-none`)
- LibreOffice 26.2.3.2 at `C:\Program Files\LibreOffice\program\soffice.exe`
- openpyxl 3.1.5
- `anthropics/skills` xlsx skill at current `main`

## Reproduction

1. Build any `.xlsx` with openpyxl that contains formulas.
2. Run `python skills/xlsx/scripts/recalc.py `.

**Actual:**

```
Traceback (most recent call last):
File ".../skills/xlsx/scripts/recalc.py", line 184, in
main()
File ".../skills/xlsx/scripts/recalc.py", line 179, in main
result = recalc(filename, timeout)
File ".../skills/xlsx/scripts/recalc.py", line 76, in recalc
if not setup_libreoffice_macro():
File ".../skills/xlsx/scripts/recalc.py", line 59, in setup_libreoffice_macro
env=get_soffice_env(),
File ".../skills/xlsx/scripts/office/soffice.py", line 28, in get_soffice_env
if _needs_shim():
File ".../skills/xlsx/scripts/office/soffice.py", line 46, in _needs_shim
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
^^^^^^^^^^^^^^
AttributeError: module 'socket' has no attribute 'AF_UNIX'
```

**Expected:** `_needs_shim()` returns `False` on platforms where AF_UNIX is unavailable. The shim itself is Linux-specific (uses `LD_PRELOAD` and a gcc-compiled `.so`), so there is nothing meaningful to shim on Windows. `recalc.py` should then proceed to drive LibreOffice normally.

## Root cause

`_needs_shim()` only catches `OSError`:

```python
def _needs_shim() -> bool:
try:
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.close()
return False
except OSError:
return True
```

On Windows Python builds without AF_UNIX support, `socket.AF_UNIX` is missing as an *attribute* entirely, so the lookup raises `AttributeError` before the constructor runs. AF_UNIX availability on Windows is conditional on both Windows version (10 1803+) and how the interpreter was built — it is not universal even on supported Windows versions.

## Proposed fix

Two-line `hasattr` guard at the top of `_needs_shim()`:

```python
def _needs_shim() -> bool:
if not hasattr(socket, "AF_UNIX"):
return False
try:
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.close()
return False
except OSError:
return True
```

Returning `False` early when AF_UNIX is unavailable is correct behavior — `_ensure_shim()` would in any case fail on Windows because it shells out to `gcc` to compile a Linux `.so`, and `LD_PRELOAD` is a Linux-only loader hint.

Optionally also catch `AttributeError` in the existing `except` for defense-in-depth, though the `hasattr` guard is cleaner and more explicit about intent.

## Workaround

Affected users can drive LibreOffice directly, bypassing the skill's macro path:

```
soffice --headless -env:UserInstallation=file:/// --calc --convert-to xlsx --outdir
```

Confirmed working on the affected environment. The `--convert-to xlsx` round-trip forces a recalculate-and-resave, achieving the same outcome as the skill's macro-driven recalc.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.