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

CUPS cannot print to network printer due to mdns timeouts #118628

Closed
wentasah opened this issue Apr 5, 2021 · 17 comments · Fixed by #279433
Closed

CUPS cannot print to network printer due to mdns timeouts #118628

wentasah opened this issue Apr 5, 2021 · 17 comments · Fixed by #279433
Labels

Comments

@wentasah
Copy link
Contributor

wentasah commented Apr 5, 2021

Describe the bug

When printing to Brother MFC-L2700DW printer (with this package created from a similar model in nixpkgs), CUPS often (but not always) complains about "Impossible to connect to printerCCB4208.local: Name or service not known" even though I followed the instructions from NixOS wiki and enabled services.avahi.nssmdns.

To Reproduce
Steps to reproduce the behavior:

  1. Get Brother MFC-L2700DW printer.
  2. Configure NixOS as follows:
    {
      services.printing.enable = true;
      services.printing.drivers = with pkgs; [
        mfcl2700dwlpr
        mfcl2700dwcupswrapper
      ];
      services.avahi.enable = true;
      # Important to resolve .local domains of printers, otherwise you get an error
      # like  "Impossible to connect to XXX.local: Name or service not known"
      services.avahi.nssmdns = true;
    }
    (full configuration available here)
  3. Print something
  4. Browse to http://localhost:631/printers/Brother_MFC-L2700DW_series and see the "Impossible to connect to printerCCB4208.local: Name or service not known" message.

Expected behavior
The job is sent to the printer and printed.

Additional context
When the printer cannot be connected, running systemctl restart cups nscd usually helps and the stuck job gets to the printer.

After my investigation, I think that the cause is long delay in mDNS name resolution. By default NixOS uses mdns NSS module, which tries to resolve both IPv4 and IPv6 addresses sequentially. It seems to start with IPv6, which does not resolve to a valid address so after 5s timeout the resolution continues with IPv4. So the address of the printer is returned after slightly more that 5 seconds (can be verified with time getent hosts printerCCB4208.local), which seems to be too long for CUPS so it gives up and tries again later and the same repeats, because nscd in NixOS does not cache the results.

My current solution to the problem is to switch from mdns NSS to mdns4 with the following in my configuration.nix:

{
  services.avahi.nssmdns = false; # Use my settings from below
  # settings from avahi-daemon.nix where mdns is replaced with mdns4
  system.nssModules = with pkgs.lib; optional (!config.services.avahi.nssmdns) pkgs.nssmdns;
  system.nssDatabases.hosts = with pkgs.lib; optionals (!config.services.avahi.nssmdns) (mkMerge [
    (mkOrder 900 [ "mdns4_minimal [NOTFOUND=return]" ]) # must be before resolve
    (mkOrder 1501 [ "mdns4" ]) # 1501 to ensure it's after dns
  ]);
}

I've also tried services.avahi.ipv6 = false; but it didn't help.

The question is whether to make it easier for others. Possible things that come to my mind:

  • Document this at Wiki
  • Extend avahi-daemon.nix to provide e.g. nssmdns4 option.
  • Enable caching in nscd
  • Something else?

Notify maintainers

@matthewbauer @andir

Metadata
Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

  • system: "x86_64-linux"
  • host os: Linux 5.4.108, NixOS, 21.05pre279456.04a2b269d89 (Okapi)
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.3.10
  • channels(root): "nixos-21.05pre279456.04a2b269d89, nixos-hardware"
  • channels(wsh): "home-manager"
  • nixpkgs: /home/wsh/nix/nixpkgs

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute:
- cups
# a list of nixos modules affected by the problem
module:
- avahi-daemon
@wentasah wentasah added the 0.kind: bug Something is broken label Apr 5, 2021
@tex
Copy link
Contributor

tex commented May 11, 2021

Confirm, I have the same problem (with Xerox WorkCentre). And your workaround fixed it for me too. Thanks!

@wentasah
Copy link
Contributor Author

I wanted to link this issue from the wiki and found that @juanibiapina added there a comment suggesting that he might have the same problem. @juanibiapina, does the above workaround make your setup work without restarting cups after booting?

If yes, I'd update your comment, because it's not accurate. The nssmdns is not needed, because it is pulled in by services.avahi.nssmdns = true; mentioned on the previous line.

@juanibiapina
Copy link
Contributor

Hi, I'm back and I'll test your workaround soon.

About nssmdns, I thought it should have been pulled, but it wasn't at the time. I vaguely remember triple checking and it wasn't there.

@juanibiapina
Copy link
Contributor

Sorry, I can't test it again. My printer is busted.

@wentasah
Copy link
Contributor Author

OK, thanks for letting me know :-)

@juanibiapina
Copy link
Contributor

Ok, my printer has sudently revived and this solves the issue for me as well.

juanibiapina added a commit to juanibiapina/dotfiles that referenced this issue May 18, 2021
@stale
Copy link

stale bot commented Nov 16, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Nov 16, 2021
@wentasah
Copy link
Contributor Author

Confirming that the issue still exists with current nixos-unstable.

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Nov 19, 2021
@tobiasBora
Copy link
Contributor

Out of curiosity, is this workaround supposed to break or use outdated softwares? If not, would it make sense to directly apply your workaround by default?

@wentasah
Copy link
Contributor Author

This workaround makes mDNS working only for IPv4. If you are on a IPv6-only network, mDNS won't work for you. On mixed IPv4/IPv6 network, you might not see some devices. That's definitely not what some people want :-)

Btw, #146406 may be relevant to this problem. I've not read all the comments there, but the keywords seem similar to what I remember when debugging this issue.

I think the solution might be to increase or decrease some timeout somewhere, but I'm not sure where. More debugging/analysis is needed.

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/still-cant-print-after-6-months-canon-mf445dw-at-the-end-of-my-rope/24665/7

@pedrovelho
Copy link

Same happening here with a Canon MF643C. I found a workaround, but yours seems better. Mine was to manually add the name.local expected on '/etc/hosts' file. This only works if your printer has a fixed ip (like 10.1.2.3).

networking.extraHosts =
''
10.1.2.3 CanonMF643C.local
'';

@googleson78
Copy link

I also hit this issue. Given that (I assume) most people are running ipv4, can we not try the v4 exclusive module first, and still fallback to the one that also works with v6, so that it "just works" for this rather common use case?

Given, that will slowdown v6 name resolution, but I guess we could also add a big warning in the option.

@Ten0
Copy link
Contributor

Ten0 commented Jan 3, 2024

Looks like this is closed by bba808d

  services.printing.enable = true;
  services.avahi.enable = true;
  services.avahi.nssmdns4 = true;

@wentasah
Copy link
Contributor Author

wentasah commented Jan 3, 2024

Unfortunately, that commit is not sufficient for me. Current version of CUPS complains with:

"No destination host name supplied by cups-browsed for printer "Brother_MFC_L2700DW_series_mDNS", is cups-browsed running?"

The referenced commit makes my /etc/nsswitch.conf contain the following line:

hosts:     mymachines mdns4_minimal [NOTFOUND=return] files myhostname dns mdns

With it, running getent hosts printerCCB4509.local returns an IPv6 address:

fd76:76f3:2dcb:0:3205:5cff:fecb:4509 printerCCB4509.local

From the output of strace -f -p $(pidof nsncd) it seems that the command first asks for IPv6 address, so the mdns4_minimal is somehow ignored and the address is returned by mdns at the end of the line. I can ping and connect that address via nc so I don't know why CUPS cannot print to it.

Anyway, if I disable IPv6 in the printer, getent hosts returns the IPv4 address, but only after the 5 second timeout, which is too long for CUPS.

What I need is mdns4 also at the end of nsswitch.conf line. I'll ask at #258424 and submit a PR to do that if people do not complain.

@SuperSandro2000
Copy link
Member

Please submit a PR.

wentasah added a commit to wentasah/nixpkgs that referenced this issue Jan 7, 2024
…mdns source

This is a followup to commit bba808d ("nixos/avahi-daemon:
resolve mdns only over enabled protocols, disable ipv6 by default",
2023-10-01, PR NixOS#258424). mdns occurs twice in /etc/nsswitch.conf but
that commit changed only the first one (mdns_minimal, before resolve).
This commits ensures that both occurrences are set consistently.

This is not only consistent with upstream example[1] but it also
fixes NixOS#118628 -- a longstanding issue with CUPS and printer detection.

[1]  https://github.com/avahi/nss-mdns#activation
@adingbatponder
Copy link

https://fosstodon.org/@adingbatponder/111716425335189702

Uses a direct printer install. Same sort of problems encountered.

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

Successfully merging a pull request may close this issue.