NEW YORK — Serena Williams flubbed yet another shot and wailed, “I keep missing my forehand!”

She was in trouble, if only briefly, against 17-year-old American Caty McNally, who is friends, and doubles partners, with Coco Gauff.

Making key adjustments to her serve and straightening out her other strokes, Williams avoided what would have been her earliest loss in 19 appearances at the U.S. Open, coming back to beat McNally 5-7, 6-3, 6-1 in a match that ended as Wednesday turned to Thursday.

“You can’t win tournaments making that many errors,” Williams said. “I knew I had to play better, and I knew I could.”

And so she did.

“Obviously, she’s going to pick up her level. I knew that was going to happen,” McNally said. “Next time, I just have to raise my level with hers.”

Williams improved to 38-0 in the first two rounds at Flushing Meadows. The only time she was beaten as early as even the third round in New York was in her tournament debut all way back in 1998 — when she was just 16 herself.

The following year, Williams won the first of her six U.S. Open championships. McNally hadn’t even been born yet.

Now 37, Williams owns 23 Grand Slam singles titles in all, and she showed off why while powering her way through a deficit, taking 16 of the final set’s initial 17 points.

McNally had never won a match at any major tournament until Monday. She is ranked 121st and received a wild-card invitation from the U.S. Tennis Association for singles and for doubles, the latter with 15-year-old sensation Gauff.

It was Gauff who beat Williams’ older sister, Venus, on the way to the fourth round of Wimbledon last month.

Might another stunner be in the offing? Seemed a possibility for a set, anyway, with McNally charging the net, serving-and-volleying, and looking like someone who belonged.

Maybe that’s why Williams never looked comfortable early. Took time to get into a real groove. Went stretches without being the dominant force she’s been for a couple of decades and sure was just the other night while absolutely overwhelming five-time major champion Maria Sharapova 6-1, 6-1 in the same arena.

The Arthur Ashe Stadium crowd, made louder than usual under the retractable roof that was closed all day because of showers, alternated who it was pulling for, more than happy to cheer for either woman representing the United States at the country’s Grand Slam tournament.

Who doesn’t love to support an all-time great, after all? And who doesn’t enjoy getting behind a true underdog?

”She’s young. It’s her first time in a stage like this and only her second Grand Slam, so I think there’s a lot to gain from it,” said Lynn Nabors-McNally, Caty’s mother, who also helps coach her. “It’s a great stepping stone to a lot of things.”

So there was McNally, almost strutting to the sideline while waving her arms, trying to get the spectators to offer even more noise and more applause after she pulled out the opening set in impressive fashion. First, McNally converted her only break point of the match to lead 6-5. Then she served out the set despite falling behind love-40, erasing three break points and hitting a 103-mph service winner to seal it — and implored the fans to get loud.

They obliged.

“I knew I was playing against the greatest of all time. ... I walked out there and I had the chills,” McNally said. “Super happy just to get a set from her. That’s something that not very many people do.”

And for a bit of the second set, too, McNally stayed with Williams.

But Williams started to pull away, in part by dispensing with the serves out wide that McNally was handling well, and in part by reducing her mistakes from 15 unforced errors in the first set, to 11 in the second to two in the third.

“Actually, I’d rather not be tested in every match. But that doesn’t happen. So it’s important for me to have those, like, really rough, rowdy matches,” Williams said. “That helps a lot.”

On the men’s side, Novak Djokovic is through to the third round and will use some of the time until then getting more treatment on his left shoulder.

Djokovic fought through trouble with the shoulder to beat Juan Ignacio Londero 6-4, 7-6 (3), 6-1.

The defending champion was treated by the trainer multiple times in the match and appeared in trouble when he fell behind 3-0 in the second set. But he battled back to take it in a tiebreaker and had an easy time in the third set.

The No. 1 seed wouldn’t reveal much about the nature of the injury in his post-match interview.

Earlier, Venus Williams needed a little pick-me-up on a rainy day in New York.

It takes more than caffeine to beat Elina Svitolina, though.

Williams had a resurgence after some coffee was sent her way after dropping the first set against the No. 5 seed, but Svitolina charged back and eventually beat Williams 6-4, 6-4 in a second-round match.

The cup of coffee was delivered from Williams’ team in the stands to a ballboy, who tried to bring it to Williams. But she walked off to the court between sets before he could get it to her, so he eventually dropped it off next to her seat.

Williams then came back to take a 3-0 lead to start the second. But having to save four break points for a tough hold in that third game seemed to take something out of the 39-year-old Williams, as Svitolina came right back to take five straight games for a 5-3 lead.

Williams then fought off five match points in a 22-point game to hold her serve before Svitolina eventually ended it on her sixth chance, improving to 13-3 in Grand Slam matches this season.

In another early match, Roger Federer once again came back from a set down to advance, beating Damir Dzumhur 3-6, 6-2, 6-3, 6-4.

The second-round match was very much like the opener for the third-seeded Federer, who dropped the first set against qualifier Sumit Nagal before cruising the rest of the way. Federer again stepped up his game against the 99th-ranked Dzumhur, winning 77 percent of his first-serve points and blasting 58 winners, more than double that of his opponent.

The five-time U.S. Open champion is now 19-0 in second-round matches at Flushing Meadows and moves on to play the winner of the match between 25th-seeded Lucas Pouille and Daniel Evans.